5 * - General arch-specific stuff
8 #include <threads_int.h>
13 #define DEBUG_TO_SERIAL 1
14 #define SERIAL_PORT 0x3F8
15 #define GDB_SERIAL_PORT 0x2F8
19 extern struct sShortSpinlock glDebug_Lock;
20 extern struct sShortSpinlock glThreadListLock;
22 extern int GetCPUNum(void);
25 Uint64 __udivdi3(Uint64 Num, Uint64 Den);
26 Uint64 __umoddi3(Uint64 Num, Uint64 Den);
29 int gbDebug_SerialSetup = 0;
30 int gbGDB_SerialSetup = 0;
34 * \brief Determine if a short spinlock is locked
35 * \param Lock Lock pointer
37 int IS_LOCKED(struct sShortSpinlock *Lock)
43 * \brief Check if the current CPU has the lock
44 * \param Lock Lock pointer
46 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
48 return Lock->Lock == GetCPUNum() + 1;
52 * \brief Acquire a Short Spinlock
53 * \param Lock Lock pointer
55 * This type of mutex should only be used for very short sections of code,
56 * or in places where a Mutex_* would be overkill, such as appending
57 * an element to linked list (usually two assignement lines in C)
59 * \note This type of lock halts interrupts, so ensure that no timing
60 * functions are called while it is held. As a matter of fact, spend as
61 * little time as possible with this lock held
62 * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
64 void SHORTLOCK(struct sShortSpinlock *Lock)
68 int cpu = GetCPUNum() + 1;
70 // Save interrupt state
71 __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
72 IF &= 0x200; // AND out all but the interrupt flag
75 if( Lock != &glDebug_Lock && Lock != &glThreadListLock )
77 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
78 Debug("%p obtaining %p (Called by %p)", __builtin_return_address(0), Lock, __builtin_return_address(1));
82 // Wait for another CPU to release
84 __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(cpu),"D"(&Lock->Lock));
91 if( Lock != &glDebug_Lock && Lock != &glThreadListLock )
93 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
94 //Debug("Lock %p locked by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1));
100 * \brief Release a short lock
101 * \param Lock Lock pointer
103 void SHORTREL(struct sShortSpinlock *Lock)
106 if( Lock != &glDebug_Lock && Lock != &glThreadListLock )
108 //Log_Log("LOCK", "%p released by %p", Lock, __builtin_return_address(0));
109 Debug("Lock %p released by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1));
113 // Lock->IF can change anytime once Lock->Lock is zeroed
125 int putDebugChar(char ch)
127 if(!gbGDB_SerialSetup) {
128 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
129 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
130 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
131 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
132 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
133 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
134 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
135 gbGDB_SerialSetup = 1;
137 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
138 outb(GDB_SERIAL_PORT, ch);
141 int getDebugChar(void)
143 if(!gbGDB_SerialSetup) {
144 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
145 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
146 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
147 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
148 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
149 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
150 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
151 gbGDB_SerialSetup = 1;
153 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
154 return inb(GDB_SERIAL_PORT);
156 #endif /* USE_GDB_STUB */
158 void Debug_PutCharDebug(char ch)
161 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
165 if(!gbDebug_SerialSetup) {
166 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
167 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
168 outb(SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
169 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
170 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
171 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
172 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
173 gbDebug_SerialSetup = 1;
175 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
176 outb(SERIAL_PORT, ch);
180 void Debug_PutStringDebug(const char *String)
183 Debug_PutCharDebug(*String++);
186 // === IO Commands ===
187 void outb(Uint16 Port, Uint8 Data)
189 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
191 void outw(Uint16 Port, Uint16 Data)
193 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
195 void outd(Uint16 Port, Uint32 Data)
197 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
199 Uint8 inb(Uint16 Port)
202 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
205 Uint16 inw(Uint16 Port)
208 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
211 Uint32 ind(Uint16 Port)
214 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
219 * \fn void *memset(void *Dest, int Val, size_t Num)
220 * \brief Do a byte granuality set of Dest
222 void *memset(void *Dest, int Val, size_t Num)
224 Uint32 val = Val&0xFF;
227 __asm__ __volatile__ (
231 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
235 * \brief Set double words
237 void *memsetd(void *Dest, Uint32 Val, size_t Num)
239 __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
244 * \fn int memcmp(const void *m1, const void *m2, size_t Num)
245 * \brief Compare two pieces of memory
247 int memcmp(const void *m1, const void *m2, size_t Num)
249 const Uint8 *d1 = m1;
250 const Uint8 *d2 = m2;
251 if( Num == 0 ) return 0; // No bytes are always identical
264 * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
265 * \brief Copy \a Num bytes from \a Src to \a Dest
267 void *memcpy(void *Dest, const void *Src, size_t Num)
269 tVAddr dst = (tVAddr)Dest;
270 tVAddr src = (tVAddr)Src;
271 if( (dst & 3) != (src & 3) )
273 __asm__ __volatile__ ("rep movsb" :: "D" (dst), "S" (src), "c" (Num));
274 // Debug("\nmemcpy:Num=0x%x by %p (UA)", Num, __builtin_return_address(0));
277 else if( Num > 128 && (dst & 15) == (src & 15) )
279 char tmp[16+15]; // Note, this is a hack to save/restor xmm0
280 int count = 16 - (dst & 15);
281 // Debug("\nmemcpy:Num=0x%x by %p (SSE)", Num, __builtin_return_address(0));
285 __asm__ __volatile__ ("rep movsb" : "=D"(dst),"=S"(src): "0"(dst), "1"(src), "c"(count));
289 __asm__ __volatile__ (
290 "movdqa 0(%5), %%xmm0;\n\t"
292 "movdqa 0(%1), %%xmm0;\n\t"
293 "movdqa %%xmm0, 0(%0);\n\t"
297 "movdqa %%xmm0, 0(%5);\n\t"
298 : "=r"(dst),"=r"(src)
299 : "0"(dst), "1"(src), "c"(count), "r" (((tVAddr)tmp+15)&~15)
304 __asm__ __volatile__ ("rep movsb" :: "D"(dst), "S"(src), "c"(count));
309 // Debug("\nmemcpy:Num=0x%x by %p", Num, __builtin_return_address(0));
310 __asm__ __volatile__ (
314 :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
320 * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
321 * \brief Copy \a Num DWORDs from \a Src to \a Dest
323 void *memcpyd(void *Dest, const void *Src, size_t Num)
325 __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
329 Uint64 DivMod64U(Uint64 Num, Uint64 Div, Uint64 *Rem)
332 if( Div < 0x100000000ULL && Num < 0xFFFFFFFF * Div ) {
334 __asm__ __volatile__(
336 : "=a" (ret_32), "=d" (rem)
337 : "a" ( (Uint32)(Num & 0xFFFFFFFF) ), "d" ((Uint32)(Num >> 32)), "r" (Div)
343 ret = __udivdi3(Num, Div);
344 if(Rem) *Rem = __umoddi3(Num, Div);
349 * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
350 * \brief Divide two 64-bit integers
352 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
358 if(Den == 0) __asm__ __volatile__ ("int $0x0");
360 if(Num <= 0xFFFFFFFF && Den <= 0xFFFFFFFF)
361 return (Uint32)Num / (Uint32)Den;
362 if(Den == 1) return Num;
363 if(Den == 2) return Num >> 1; // Speed Hacks
364 if(Den == 4) return Num >> 2; // Speed Hacks
365 if(Den == 8) return Num >> 3; // Speed Hacks
366 if(Den == 16) return Num >> 4; // Speed Hacks
367 if(Den == 32) return Num >> 5; // Speed Hacks
368 if(Den == 1024) return Num >> 10; // Speed Hacks
369 if(Den == 2048) return Num >> 11; // Speed Hacks
370 if(Den == 4096) return Num >> 12;
371 if(Num < Den) return 0;
372 if(Num < Den*2) return 1;
373 if(Num == Den*2) return 2;
379 __asm__ __volatile__ (
380 "fildq %2\n\t" // Num
381 "fildq %1\n\t" // Den
385 : "m" (P[0]), "m" (P[1])
388 //Log("%llx / %llx = %llx\n", Num, Den, q);
390 // Restoring division, from wikipedia
391 // http://en.wikipedia.org/wiki/Division_(digital)
392 P[0] = Num; P[1] = 0;
396 P[1] = (P[1] << 1) | (P[0] >> 63);
403 if( !(P[1] & (1ULL<<63)) ) {
404 q |= (Uint64)1 << (63-i);
417 * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
418 * \brief Get the modulus of two 64-bit integers
420 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
422 if(Den == 0) __asm__ __volatile__ ("int $0x0"); // Call Div by Zero Error
423 if(Den == 1) return 0; // Speed Hacks
424 if(Den == 2) return Num & 1; // Speed Hacks
425 if(Den == 4) return Num & 3; // Speed Hacks
426 if(Den == 8) return Num & 7; // Speed Hacks
427 if(Den == 16) return Num & 15; // Speed Hacks
428 if(Den == 32) return Num & 31; // Speed Hacks
429 if(Den == 1024) return Num & 1023; // Speed Hacks
430 if(Den == 2048) return Num & 2047; // Speed Hacks
431 if(Den == 4096) return Num & 4095; // Speed Hacks
433 if(Num >> 32 == 0 && Den >> 32 == 0)
434 return (Uint32)Num % (Uint32)Den;
436 return Num - __udivdi3(Num, Den) * Den;
441 EXPORT(memcpy); EXPORT(memset);
443 //EXPORT(memcpyw); EXPORT(memsetw);
444 EXPORT(memcpyd); EXPORT(memsetd);
445 EXPORT(inb); EXPORT(inw); EXPORT(ind);
446 EXPORT(outb); EXPORT(outw); EXPORT(outd);
447 EXPORT(__udivdi3); EXPORT(__umoddi3);