5 * - General arch-specific stuff
8 #include <threads_int.h>
10 #include <hal_proc.h> // GetCPUNum
11 #include <drv_serial.h>
16 #define DEBUG_TO_SERIAL 1
17 #define SERIAL_PORT 0x3F8
18 #define GDB_SERIAL_PORT 0x2F8
22 extern struct sShortSpinlock glDebug_Lock;
23 extern tMutex glPhysAlloc;
24 #define TRACE_LOCK_COND (Lock != &glDebug_Lock && Lock != &glThreadListLock && Lock != &glPhysAlloc.Protector)
25 //#define TRACE_LOCK_COND (Lock != &glDebug_Lock && Lock != &glPhysAlloc.Protector)
29 Uint64 __divmod64(Uint64 Num, Uint64 Den, Uint64 *Rem);
30 Uint64 __udivdi3(Uint64 Num, Uint64 Den);
31 Uint64 __umoddi3(Uint64 Num, Uint64 Den);
32 void Debug_SerialIRQHandler(int irq, void *unused);
35 int gbDebug_SerialSetup = 0;
36 int gbGDB_SerialSetup = 0;
40 * \brief Determine if a short spinlock is locked
41 * \param Lock Lock pointer
43 int IS_LOCKED(struct sShortSpinlock *Lock)
49 * \brief Check if the current CPU has the lock
50 * \param Lock Lock pointer
52 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
54 return Lock->Lock == GetCPUNum() + 1;
57 void __AtomicTestSetLoop(Uint *Ptr, Uint Value)
61 "xor %%eax, %%eax;\n\t"
62 "lock cmpxchgl %0, (%1);\n\t"
64 :: "r"(Value), "r"(Ptr)
65 : "eax" // EAX clobbered
69 * \brief Acquire a Short Spinlock
70 * \param Lock Lock pointer
72 * This type of mutex should only be used for very short sections of code,
73 * or in places where a Mutex_* would be overkill, such as appending
74 * an element to linked list (usually two assignement lines in C)
76 * \note This type of lock halts interrupts, so ensure that no timing
77 * functions are called while it is held. As a matter of fact, spend as
78 * little time as possible with this lock held
79 * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
81 void SHORTLOCK(struct sShortSpinlock *Lock)
84 int cpu = GetCPUNum() + 1;
86 // Save interrupt state
87 __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
88 IF &= 0x200; // AND out all but the interrupt flag
90 if( CPU_HAS_LOCK(Lock) )
92 Panic("Double lock of %p, %p req, %p has", Lock, __builtin_return_address(0), Lock->LockedBy);
99 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
100 Debug("%i %p obtaining %p (Called by %p)", cpu-1, __builtin_return_address(0), Lock, __builtin_return_address(1));
106 // Wait for another CPU to release
107 __AtomicTestSetLoop( (Uint*)&Lock->Lock, cpu );
109 Lock->LockedBy = __builtin_return_address(0);
112 if( TRACE_LOCK_COND )
114 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
115 Debug("%i %p locked by %p\t%p", cpu-1, Lock, __builtin_return_address(0), __builtin_return_address(1));
121 * \brief Release a short lock
122 * \param Lock Lock pointer
124 void SHORTREL(struct sShortSpinlock *Lock)
127 if( TRACE_LOCK_COND )
129 //Log_Log("LOCK", "%p released by %p", Lock, __builtin_return_address(0));
130 Debug("Lock %p released by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1));
134 // Lock->IF can change anytime once Lock->Lock is zeroed
146 int putDebugChar(char ch)
148 if(!gbGDB_SerialSetup) {
149 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
150 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
151 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
152 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
153 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
154 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
155 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
156 gbGDB_SerialSetup = 1;
158 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
159 outb(GDB_SERIAL_PORT, ch);
162 int getDebugChar(void)
164 if(!gbGDB_SerialSetup) {
165 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
166 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
167 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
168 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
169 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
170 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
171 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
172 gbGDB_SerialSetup = 1;
174 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
175 return inb(GDB_SERIAL_PORT);
177 #endif /* USE_GDB_STUB */
179 void Debug_PutCharDebug(char ch)
182 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
186 if(!gbDebug_SerialSetup) {
187 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
188 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
189 outb(SERIAL_PORT + 0, 0x01); // Set divisor to 1 (lo byte) - 115200 baud
190 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
191 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
192 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
193 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
194 outb(SERIAL_PORT + 1, 0x05); // Enable ERBFI (Rx Full), ELSI (Line Status)
195 gbDebug_SerialSetup = 1;
196 IRQ_AddHandler(4, Debug_SerialIRQHandler, NULL);
198 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
199 outb(SERIAL_PORT, ch);
203 void Debug_PutStringDebug(const char *String)
206 Debug_PutCharDebug(*String++);
209 void Debug_SerialIRQHandler(int irq, void *unused)
211 if( (inb(SERIAL_PORT+5) & 0x01) == 0 ) {
212 Debug("IRQ4, no data");
216 char ch = inb(SERIAL_PORT);
217 Serial_ByteReceived(gSerial_KernelDebugPort, ch);
220 // === IO Commands ===
221 void outb(Uint16 Port, Uint8 Data)
223 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
225 void outw(Uint16 Port, Uint16 Data)
227 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
229 void outd(Uint16 Port, Uint32 Data)
231 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
233 Uint8 inb(Uint16 Port)
236 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
239 Uint16 inw(Uint16 Port)
242 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
245 Uint32 ind(Uint16 Port)
248 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
253 * \fn void *memset(void *Dest, int Val, size_t Num)
254 * \brief Do a byte granuality set of Dest
256 void *memset(void *Dest, int Val, size_t Num)
258 Uint32 val = Val&0xFF;
261 __asm__ __volatile__ (
265 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
269 * \brief Set double words
271 void *memsetd(void *Dest, Uint32 Val, size_t Num)
273 __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
278 * \fn int memcmp(const void *m1, const void *m2, size_t Num)
279 * \brief Compare two pieces of memory
281 int memcmp(const void *m1, const void *m2, size_t Num)
283 const Uint8 *d1 = m1;
284 const Uint8 *d2 = m2;
285 if( Num == 0 ) return 0; // No bytes are always identical
298 * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
299 * \brief Copy \a Num bytes from \a Src to \a Dest
301 void *memcpy(void *Dest, const void *Src, size_t Num)
303 tVAddr dst = (tVAddr)Dest;
304 tVAddr src = (tVAddr)Src;
305 if( (dst & 3) != (src & 3) )
307 __asm__ __volatile__ ("rep movsb" :: "D" (dst), "S" (src), "c" (Num));
308 // Debug("\nmemcpy:Num=0x%x by %p (UA)", Num, __builtin_return_address(0));
311 else if( Num > 128 && (dst & 15) == (src & 15) )
313 char tmp[16+15]; // Note, this is a hack to save/restor xmm0
314 int count = 16 - (dst & 15);
315 // Debug("\nmemcpy:Num=0x%x by %p (SSE)", Num, __builtin_return_address(0));
319 __asm__ __volatile__ ("rep movsb" : "=D"(dst),"=S"(src): "0"(dst), "1"(src), "c"(count));
323 __asm__ __volatile__ (
324 "movdqa 0(%5), %%xmm0;\n\t"
326 "movdqa 0(%1), %%xmm0;\n\t"
327 "movdqa %%xmm0, 0(%0);\n\t"
331 "movdqa %%xmm0, 0(%5);\n\t"
332 : "=r"(dst),"=r"(src)
333 : "0"(dst), "1"(src), "c"(count), "r" (((tVAddr)tmp+15)&~15)
338 __asm__ __volatile__ ("rep movsb" :: "D"(dst), "S"(src), "c"(count));
343 // Debug("\nmemcpy:Num=0x%x by %p", Num, __builtin_return_address(0));
344 __asm__ __volatile__ (
348 :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
354 * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
355 * \brief Copy \a Num DWORDs from \a Src to \a Dest
357 void *memcpyd(void *Dest, const void *Src, size_t Num)
359 __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
363 #include "../helpers.h"
367 Uint64 DivMod64U(Uint64 Num, Uint64 Div, Uint64 *Rem)
370 if(Rem) *Rem = Num & 15;
373 if( Div < 0x100000000ULL && Num < 0xFFFFFFFF * Div ) {
375 __asm__ __volatile__(
377 : "=a" (ret_32), "=d" (rem)
378 : "a" ( (Uint32)(Num & 0xFFFFFFFF) ), "d" ((Uint32)(Num >> 32)), "r" (Div)
384 return __divmod64(Num, Div, Rem);
388 * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
389 * \brief Divide two 64-bit integers
391 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
394 __asm__ __volatile__ ("int $0x0");
398 if(Num <= 0xFFFFFFFF && Den <= 0xFFFFFFFF)
399 return (Uint32)Num / (Uint32)Den;
400 if(Den == 1) return Num;
401 if(Den == 2) return Num >> 1; // Speed Hacks
402 if(Den == 4) return Num >> 2; // Speed Hacks
403 if(Den == 8) return Num >> 3; // Speed Hacks
404 if(Den == 16) return Num >> 4; // Speed Hacks
405 if(Den == 32) return Num >> 5; // Speed Hacks
406 if(Den == 1024) return Num >> 10; // Speed Hacks
407 if(Den == 2048) return Num >> 11; // Speed Hacks
408 if(Den == 4096) return Num >> 12;
409 if(Num < Den) return 0;
410 if(Num < Den*2) return 1;
411 if(Num == Den*2) return 2;
413 return __divmod64(Num, Den, NULL);
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)
424 __asm__ __volatile__ ("int $0x0"); // Call Div by Zero Error
427 if(Den == 1) return 0; // Speed Hacks
428 if(Den == 2) return Num & 1; // Speed Hacks
429 if(Den == 4) return Num & 3; // Speed Hacks
430 if(Den == 8) return Num & 7; // Speed Hacks
431 if(Den == 16) return Num & 15; // Speed Hacks
432 if(Den == 32) return Num & 31; // Speed Hacks
433 if(Den == 1024) return Num & 1023; // Speed Hacks
434 if(Den == 2048) return Num & 2047; // Speed Hacks
435 if(Den == 4096) return Num & 4095; // Speed Hacks
437 if(Num >> 32 == 0 && Den >> 32 == 0)
438 return (Uint32)Num % (Uint32)Den;
440 __divmod64(Num, Den, &ret);
446 EXPORT(memcpy); EXPORT(memset);
448 //EXPORT(memcpyw); EXPORT(memsetw);
449 EXPORT(memcpyd); EXPORT(memsetd);
450 EXPORT(inb); EXPORT(inw); EXPORT(ind);
451 EXPORT(outb); EXPORT(outw); EXPORT(outd);
452 EXPORT(__udivdi3); EXPORT(__umoddi3);