5 * - General arch-specific stuff
8 #include <threads_int.h>
10 #include <hal_proc.h> // GetCPUNum
15 #define DEBUG_TO_SERIAL 1
16 #define SERIAL_PORT 0x3F8
17 #define GDB_SERIAL_PORT 0x2F8
21 extern struct sShortSpinlock glDebug_Lock;
22 extern tMutex glPhysAlloc;
23 #define TRACE_LOCK_COND (Lock != &glDebug_Lock && Lock != &glThreadListLock && Lock != &glPhysAlloc.Protector)
24 //#define TRACE_LOCK_COND (Lock != &glDebug_Lock && Lock != &glPhysAlloc.Protector)
28 Uint64 __divmod64(Uint64 Num, Uint64 Den, Uint64 *Rem);
29 Uint64 __udivdi3(Uint64 Num, Uint64 Den);
30 Uint64 __umoddi3(Uint64 Num, Uint64 Den);
33 int gbDebug_SerialSetup = 0;
34 int gbGDB_SerialSetup = 0;
38 * \brief Determine if a short spinlock is locked
39 * \param Lock Lock pointer
41 int IS_LOCKED(struct sShortSpinlock *Lock)
47 * \brief Check if the current CPU has the lock
48 * \param Lock Lock pointer
50 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
52 return Lock->Lock == GetCPUNum() + 1;
55 void __AtomicTestSetLoop(Uint *Ptr, Uint Value)
59 "xor %%eax, %%eax;\n\t"
60 "lock cmpxchgl %0, (%1);\n\t"
62 :: "r"(Value), "r"(Ptr)
63 : "eax" // EAX clobbered
67 * \brief Acquire a Short Spinlock
68 * \param Lock Lock pointer
70 * This type of mutex should only be used for very short sections of code,
71 * or in places where a Mutex_* would be overkill, such as appending
72 * an element to linked list (usually two assignement lines in C)
74 * \note This type of lock halts interrupts, so ensure that no timing
75 * functions are called while it is held. As a matter of fact, spend as
76 * little time as possible with this lock held
77 * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
79 void SHORTLOCK(struct sShortSpinlock *Lock)
82 int cpu = GetCPUNum() + 1;
84 // Save interrupt state
85 __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
86 IF &= 0x200; // AND out all but the interrupt flag
88 if( CPU_HAS_LOCK(Lock) )
90 Panic("Double lock of %p, %p req, %p has", Lock, __builtin_return_address(0), Lock->LockedBy);
97 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
98 Debug("%i %p obtaining %p (Called by %p)", cpu-1, __builtin_return_address(0), Lock, __builtin_return_address(1));
104 // Wait for another CPU to release
105 __AtomicTestSetLoop( (Uint*)&Lock->Lock, cpu );
107 Lock->LockedBy = __builtin_return_address(0);
110 if( TRACE_LOCK_COND )
112 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
113 Debug("%i %p locked by %p\t%p", cpu-1, Lock, __builtin_return_address(0), __builtin_return_address(1));
119 * \brief Release a short lock
120 * \param Lock Lock pointer
122 void SHORTREL(struct sShortSpinlock *Lock)
125 if( TRACE_LOCK_COND )
127 //Log_Log("LOCK", "%p released by %p", Lock, __builtin_return_address(0));
128 Debug("Lock %p released by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1));
132 // Lock->IF can change anytime once Lock->Lock is zeroed
144 int putDebugChar(char ch)
146 if(!gbGDB_SerialSetup) {
147 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
148 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
149 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
150 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
151 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
152 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
153 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
154 gbGDB_SerialSetup = 1;
156 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
157 outb(GDB_SERIAL_PORT, ch);
160 int getDebugChar(void)
162 if(!gbGDB_SerialSetup) {
163 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
164 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
165 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
166 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
167 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
168 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
169 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
170 gbGDB_SerialSetup = 1;
172 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
173 return inb(GDB_SERIAL_PORT);
175 #endif /* USE_GDB_STUB */
177 void Debug_PutCharDebug(char ch)
180 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
184 if(!gbDebug_SerialSetup) {
185 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
186 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
187 outb(SERIAL_PORT + 0, 0x01); // Set divisor to 1 (lo byte) - 115200 baud
188 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
189 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
190 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
191 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
192 gbDebug_SerialSetup = 1;
194 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
195 outb(SERIAL_PORT, ch);
199 void Debug_PutStringDebug(const char *String)
202 Debug_PutCharDebug(*String++);
205 // === IO Commands ===
206 void outb(Uint16 Port, Uint8 Data)
208 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
210 void outw(Uint16 Port, Uint16 Data)
212 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
214 void outd(Uint16 Port, Uint32 Data)
216 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
218 Uint8 inb(Uint16 Port)
221 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
224 Uint16 inw(Uint16 Port)
227 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
230 Uint32 ind(Uint16 Port)
233 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
238 * \fn void *memset(void *Dest, int Val, size_t Num)
239 * \brief Do a byte granuality set of Dest
241 void *memset(void *Dest, int Val, size_t Num)
243 Uint32 val = Val&0xFF;
246 __asm__ __volatile__ (
250 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
254 * \brief Set double words
256 void *memsetd(void *Dest, Uint32 Val, size_t Num)
258 __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
263 * \fn int memcmp(const void *m1, const void *m2, size_t Num)
264 * \brief Compare two pieces of memory
266 int memcmp(const void *m1, const void *m2, size_t Num)
268 const Uint8 *d1 = m1;
269 const Uint8 *d2 = m2;
270 if( Num == 0 ) return 0; // No bytes are always identical
283 * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
284 * \brief Copy \a Num bytes from \a Src to \a Dest
286 void *memcpy(void *Dest, const void *Src, size_t Num)
288 tVAddr dst = (tVAddr)Dest;
289 tVAddr src = (tVAddr)Src;
290 if( (dst & 3) != (src & 3) )
292 __asm__ __volatile__ ("rep movsb" :: "D" (dst), "S" (src), "c" (Num));
293 // Debug("\nmemcpy:Num=0x%x by %p (UA)", Num, __builtin_return_address(0));
296 else if( Num > 128 && (dst & 15) == (src & 15) )
298 char tmp[16+15]; // Note, this is a hack to save/restor xmm0
299 int count = 16 - (dst & 15);
300 // Debug("\nmemcpy:Num=0x%x by %p (SSE)", Num, __builtin_return_address(0));
304 __asm__ __volatile__ ("rep movsb" : "=D"(dst),"=S"(src): "0"(dst), "1"(src), "c"(count));
308 __asm__ __volatile__ (
309 "movdqa 0(%5), %%xmm0;\n\t"
311 "movdqa 0(%1), %%xmm0;\n\t"
312 "movdqa %%xmm0, 0(%0);\n\t"
316 "movdqa %%xmm0, 0(%5);\n\t"
317 : "=r"(dst),"=r"(src)
318 : "0"(dst), "1"(src), "c"(count), "r" (((tVAddr)tmp+15)&~15)
323 __asm__ __volatile__ ("rep movsb" :: "D"(dst), "S"(src), "c"(count));
328 // Debug("\nmemcpy:Num=0x%x by %p", Num, __builtin_return_address(0));
329 __asm__ __volatile__ (
333 :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
339 * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
340 * \brief Copy \a Num DWORDs from \a Src to \a Dest
342 void *memcpyd(void *Dest, const void *Src, size_t Num)
344 __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
348 #include "../helpers.h"
352 Uint64 DivMod64U(Uint64 Num, Uint64 Div, Uint64 *Rem)
354 if( Div < 0x100000000ULL && Num < 0xFFFFFFFF * Div ) {
356 __asm__ __volatile__(
358 : "=a" (ret_32), "=d" (rem)
359 : "a" ( (Uint32)(Num & 0xFFFFFFFF) ), "d" ((Uint32)(Num >> 32)), "r" (Div)
365 return __divmod64(Num, Div, Rem);
369 * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
370 * \brief Divide two 64-bit integers
372 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
375 __asm__ __volatile__ ("int $0x0");
379 if(Num <= 0xFFFFFFFF && Den <= 0xFFFFFFFF)
380 return (Uint32)Num / (Uint32)Den;
381 if(Den == 1) return Num;
382 if(Den == 2) return Num >> 1; // Speed Hacks
383 if(Den == 4) return Num >> 2; // Speed Hacks
384 if(Den == 8) return Num >> 3; // Speed Hacks
385 if(Den == 16) return Num >> 4; // Speed Hacks
386 if(Den == 32) return Num >> 5; // Speed Hacks
387 if(Den == 1024) return Num >> 10; // Speed Hacks
388 if(Den == 2048) return Num >> 11; // Speed Hacks
389 if(Den == 4096) return Num >> 12;
390 if(Num < Den) return 0;
391 if(Num < Den*2) return 1;
392 if(Num == Den*2) return 2;
394 return __divmod64(Num, Den, NULL);
398 * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
399 * \brief Get the modulus of two 64-bit integers
401 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
405 __asm__ __volatile__ ("int $0x0"); // Call Div by Zero Error
408 if(Den == 1) return 0; // Speed Hacks
409 if(Den == 2) return Num & 1; // Speed Hacks
410 if(Den == 4) return Num & 3; // Speed Hacks
411 if(Den == 8) return Num & 7; // Speed Hacks
412 if(Den == 16) return Num & 15; // Speed Hacks
413 if(Den == 32) return Num & 31; // Speed Hacks
414 if(Den == 1024) return Num & 1023; // Speed Hacks
415 if(Den == 2048) return Num & 2047; // Speed Hacks
416 if(Den == 4096) return Num & 4095; // Speed Hacks
418 if(Num >> 32 == 0 && Den >> 32 == 0)
419 return (Uint32)Num % (Uint32)Den;
421 __divmod64(Num, Den, &ret);
427 EXPORT(memcpy); EXPORT(memset);
429 //EXPORT(memcpyw); EXPORT(memsetw);
430 EXPORT(memcpyd); EXPORT(memsetd);
431 EXPORT(inb); EXPORT(inw); EXPORT(ind);
432 EXPORT(outb); EXPORT(outw); EXPORT(outd);
433 EXPORT(__udivdi3); EXPORT(__umoddi3);