Kernel - Debugging hard locks
[tpg/acess2.git] / KernelLand / Kernel / arch / x86 / lib.c
1 /*
2  * Acess2
3  *
4  * arch/x86/lib.c
5  * - General arch-specific stuff
6  */
7 #include <acess.h>
8 #include <threads_int.h>
9 #include <arch_int.h>
10 #include <hal_proc.h>   // GetCPUNum
11
12 #define TRACE_LOCKS     0
13
14 #define DEBUG_TO_E9     1
15 #define DEBUG_TO_SERIAL 1
16 #define SERIAL_PORT     0x3F8
17 #define GDB_SERIAL_PORT 0x2F8
18
19 // === IMPRORTS ===
20 #if TRACE_LOCKS
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)
25 #endif
26
27 // === PROTOTYPES ==
28 Uint64  __divmod64(Uint64 Num, Uint64 Den, Uint64 *Rem);
29 Uint64  __udivdi3(Uint64 Num, Uint64 Den);
30 Uint64  __umoddi3(Uint64 Num, Uint64 Den);
31
32 // === GLOBALS ===
33  int    gbDebug_SerialSetup = 0;
34  int    gbGDB_SerialSetup = 0;
35
36 // === CODE ===
37 /**
38  * \brief Determine if a short spinlock is locked
39  * \param Lock  Lock pointer
40  */
41 int IS_LOCKED(struct sShortSpinlock *Lock)
42 {
43         return !!Lock->Lock;
44 }
45
46 /**
47  * \brief Check if the current CPU has the lock
48  * \param Lock  Lock pointer
49  */
50 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
51 {
52         return Lock->Lock == GetCPUNum() + 1;
53 }
54
55 void __AtomicTestSetLoop(Uint *Ptr, Uint Value)
56 {
57         __ASM__(
58                 "1:\n\t"
59                 "xor %%eax, %%eax;\n\t"
60                 "lock cmpxchgl %0, (%1);\n\t"
61                 "jnz 1b;\n\t"
62                 :: "r"(Value), "r"(Ptr)
63                 : "eax" // EAX clobbered
64                 );
65 }
66 /**
67  * \brief Acquire a Short Spinlock
68  * \param Lock  Lock pointer
69  * 
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)
73  * 
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
78  */
79 void SHORTLOCK(struct sShortSpinlock *Lock)
80 {
81          int    IF;
82          int    cpu = GetCPUNum() + 1;
83         
84         // Save interrupt state
85         __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
86         IF &= 0x200;    // AND out all but the interrupt flag
87         
88         ASSERT( !CPU_HAS_LOCK(Lock) );
89
90         #if TRACE_LOCKS
91         if( TRACE_LOCK_COND )
92         {
93                 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
94                 Debug("%i %p obtaining %p (Called by %p)", cpu-1,  __builtin_return_address(0), Lock, __builtin_return_address(1));
95         }
96         #endif
97         
98         __ASM__("cli");
99         
100         // Wait for another CPU to release
101         __AtomicTestSetLoop( (Uint*)&Lock->Lock, cpu );
102         Lock->IF = IF;
103         
104         #if TRACE_LOCKS
105         if( TRACE_LOCK_COND )
106         {
107                 //Log_Log("LOCK", "%p locked by %p", Lock, __builtin_return_address(0));
108                 Debug("%i %p locked by %p\t%p", cpu-1, Lock, __builtin_return_address(0), __builtin_return_address(1));
109 //              Debug("got it");
110         }
111         #endif
112 }
113 /**
114  * \brief Release a short lock
115  * \param Lock  Lock pointer
116  */
117 void SHORTREL(struct sShortSpinlock *Lock)
118 {       
119         #if TRACE_LOCKS
120         if( TRACE_LOCK_COND )
121         {
122                 //Log_Log("LOCK", "%p released by %p", Lock, __builtin_return_address(0));
123                 Debug("Lock %p released by %p\t%p", Lock, __builtin_return_address(0), __builtin_return_address(1));
124         }
125         #endif
126         
127         // Lock->IF can change anytime once Lock->Lock is zeroed
128         if(Lock->IF) {
129                 Lock->Lock = 0;
130                 __ASM__ ("sti");
131         }
132         else {
133                 Lock->Lock = 0;
134         }
135 }
136
137 // === DEBUG IO ===
138 #if USE_GDB_STUB
139 int putDebugChar(char ch)
140 {
141         if(!gbGDB_SerialSetup) {
142                 outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
143                 outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
144                 outb(GDB_SERIAL_PORT + 0, 0x0C);    // Set divisor to 12 (lo byte) 9600 baud
145                 outb(GDB_SERIAL_PORT + 1, 0x00);    //  (base is         (hi byte)
146                 outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit (8N1)
147                 outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
148                 outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
149                 gbGDB_SerialSetup = 1;
150         }
151         while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
152         outb(GDB_SERIAL_PORT, ch);
153         return 0;
154 }
155 int getDebugChar(void)
156 {
157         if(!gbGDB_SerialSetup) {
158                 outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
159                 outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
160                 outb(GDB_SERIAL_PORT + 0, 0x0C);    // Set divisor to 12 (lo byte) 9600 baud
161                 outb(GDB_SERIAL_PORT + 1, 0x00);    //                   (hi byte)
162                 outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
163                 outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
164                 outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
165                 gbGDB_SerialSetup = 1;
166         }
167         while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0)     ;
168         return inb(GDB_SERIAL_PORT);
169 }
170 #endif  /* USE_GDB_STUB */
171
172 void Debug_PutCharDebug(char ch)
173 {
174         #if DEBUG_TO_E9
175         __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
176         #endif
177         
178         #if DEBUG_TO_SERIAL
179         if(!gbDebug_SerialSetup) {
180                 outb(SERIAL_PORT + 1, 0x00);    // Disable all interrupts
181                 outb(SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
182                 outb(SERIAL_PORT + 0, 0x01);    // Set divisor to 1 (lo byte) - 115200 baud
183                 outb(SERIAL_PORT + 1, 0x00);    //                  (hi byte)
184                 outb(SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
185                 outb(SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
186                 outb(SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
187                 gbDebug_SerialSetup = 1;
188         }
189         while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
190         outb(SERIAL_PORT, ch);
191         #endif
192 }
193
194 void Debug_PutStringDebug(const char *String)
195 {
196         while(*String)
197                 Debug_PutCharDebug(*String++);
198 }
199
200 // === IO Commands ===
201 void outb(Uint16 Port, Uint8 Data)
202 {
203         __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
204 }
205 void outw(Uint16 Port, Uint16 Data)
206 {
207         __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
208 }
209 void outd(Uint16 Port, Uint32 Data)
210 {
211         __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
212 }
213 Uint8 inb(Uint16 Port)
214 {
215         Uint8   ret;
216         __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
217         return ret;
218 }
219 Uint16 inw(Uint16 Port)
220 {
221         Uint16  ret;
222         __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
223         return ret;
224 }
225 Uint32 ind(Uint16 Port)
226 {
227         Uint32  ret;
228         __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
229         return ret;
230 }
231
232 /**
233  * \fn void *memset(void *Dest, int Val, size_t Num)
234  * \brief Do a byte granuality set of Dest
235  */
236 void *memset(void *Dest, int Val, size_t Num)
237 {
238         Uint32  val = Val&0xFF;
239         val |= val << 8;
240         val |= val << 16;
241         __asm__ __volatile__ (
242                 "rep stosl;\n\t"
243                 "mov %3, %%ecx;\n\t"
244                 "rep stosb"
245                 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
246         return Dest;
247 }
248 /**
249  * \brief Set double words
250  */
251 void *memsetd(void *Dest, Uint32 Val, size_t Num)
252 {
253         __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
254         return Dest;
255 }
256
257 /**
258  * \fn int memcmp(const void *m1, const void *m2, size_t Num)
259  * \brief Compare two pieces of memory
260  */
261 int memcmp(const void *m1, const void *m2, size_t Num)
262 {
263         const Uint8     *d1 = m1;
264         const Uint8     *d2 = m2;
265         if( Num == 0 )  return 0;       // No bytes are always identical
266         
267         while(Num--)
268         {
269                 if(*d1 != *d2)
270                         return *d1 - *d2;
271                 d1 ++;
272                 d2 ++;
273         }
274         return 0;
275 }
276
277 /**
278  * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
279  * \brief Copy \a Num bytes from \a Src to \a Dest
280  */
281 void *memcpy(void *Dest, const void *Src, size_t Num)
282 {
283         tVAddr  dst = (tVAddr)Dest;
284         tVAddr  src = (tVAddr)Src;
285         if( (dst & 3) != (src & 3) )
286         {
287                 __asm__ __volatile__ ("rep movsb" :: "D" (dst), "S" (src), "c" (Num));
288 //              Debug("\nmemcpy:Num=0x%x by %p (UA)", Num, __builtin_return_address(0));
289         }
290         #if 1
291         else if( Num > 128 && (dst & 15) == (src & 15) )
292         {
293                 char    tmp[16+15];     // Note, this is a hack to save/restor xmm0
294                  int    count = 16 - (dst & 15);
295 //              Debug("\nmemcpy:Num=0x%x by %p (SSE)", Num, __builtin_return_address(0));
296                 if( count < 16 )
297                 {
298                         Num -= count;
299                         __asm__ __volatile__ ("rep movsb" : "=D"(dst),"=S"(src): "0"(dst), "1"(src), "c"(count));
300                 }
301                 
302                 count = Num / 16;
303                 __asm__ __volatile__ (
304                         "movdqa 0(%5), %%xmm0;\n\t"
305                         "1:\n\t"
306                         "movdqa 0(%1), %%xmm0;\n\t"
307                         "movdqa %%xmm0, 0(%0);\n\t"
308                         "add $16,%0;\n\t"
309                         "add $16,%1;\n\t"
310                         "loop 1b;\n\t"
311                         "movdqa %%xmm0, 0(%5);\n\t"
312                         : "=r"(dst),"=r"(src)
313                         : "0"(dst), "1"(src), "c"(count), "r" (((tVAddr)tmp+15)&~15)
314                         );
315
316                 count = Num & 15;
317                 if(count)
318                         __asm__ __volatile__ ("rep movsb" :: "D"(dst), "S"(src), "c"(count));
319         }
320         #endif
321         else
322         {
323 //              Debug("\nmemcpy:Num=0x%x by %p", Num, __builtin_return_address(0));
324                 __asm__ __volatile__ (
325                         "rep movsl;\n\t"
326                         "mov %3, %%ecx;\n\t"
327                         "rep movsb"
328                         :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
329         }
330         return Dest;
331 }
332
333 /**
334  * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
335  * \brief Copy \a Num DWORDs from \a Src to \a Dest
336  */
337 void *memcpyd(void *Dest, const void *Src, size_t Num)
338 {
339         __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
340         return Dest;
341 }
342
343 #include "../helpers.h"
344
345 DEF_DIVMOD(64);
346
347 Uint64 DivMod64U(Uint64 Num, Uint64 Div, Uint64 *Rem)
348 {
349         if( Div < 0x100000000ULL && Num < 0xFFFFFFFF * Div ) {
350                 Uint32  rem, ret_32;
351                 __asm__ __volatile__(
352                         "div %4"
353                         : "=a" (ret_32), "=d" (rem)
354                         : "a" ( (Uint32)(Num & 0xFFFFFFFF) ), "d" ((Uint32)(Num >> 32)), "r" (Div)
355                         );
356                 if(Rem) *Rem = rem;
357                 return ret_32;
358         }
359
360         return __divmod64(Num, Div, Rem);
361 }
362
363 /**
364  * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
365  * \brief Divide two 64-bit integers
366  */
367 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
368 {
369         if(Den == 0) {
370                 __asm__ __volatile__ ("int $0x0");
371                 return -1;
372         }
373         // Common speedups
374         if(Num <= 0xFFFFFFFF && Den <= 0xFFFFFFFF)
375                 return (Uint32)Num / (Uint32)Den;
376         if(Den == 1)    return Num;
377         if(Den == 2)    return Num >> 1;        // Speed Hacks
378         if(Den == 4)    return Num >> 2;        // Speed Hacks
379         if(Den == 8)    return Num >> 3;        // Speed Hacks
380         if(Den == 16)   return Num >> 4;        // Speed Hacks
381         if(Den == 32)   return Num >> 5;        // Speed Hacks
382         if(Den == 1024) return Num >> 10;       // Speed Hacks
383         if(Den == 2048) return Num >> 11;       // Speed Hacks
384         if(Den == 4096) return Num >> 12;
385         if(Num < Den)   return 0;
386         if(Num < Den*2) return 1;
387         if(Num == Den*2)        return 2;
388
389         return __divmod64(Num, Den, NULL);
390 }
391
392 /**
393  * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
394  * \brief Get the modulus of two 64-bit integers
395  */
396 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
397 {
398         Uint64  ret = 0;
399         if(Den == 0) {
400                 __asm__ __volatile__ ("int $0x0");      // Call Div by Zero Error
401                 return -1;
402         }
403         if(Den == 1)    return 0;       // Speed Hacks
404         if(Den == 2)    return Num & 1; // Speed Hacks
405         if(Den == 4)    return Num & 3; // Speed Hacks
406         if(Den == 8)    return Num & 7; // Speed Hacks
407         if(Den == 16)   return Num & 15;        // Speed Hacks
408         if(Den == 32)   return Num & 31;        // Speed Hacks
409         if(Den == 1024) return Num & 1023;      // Speed Hacks
410         if(Den == 2048) return Num & 2047;      // Speed Hacks
411         if(Den == 4096) return Num & 4095;      // Speed Hacks
412         
413         if(Num >> 32 == 0 && Den >> 32 == 0)
414                 return (Uint32)Num % (Uint32)Den;
415         
416         __divmod64(Num, Den, &ret);
417         return ret;
418 }
419
420
421 // --- EXPORTS ---
422 EXPORT(memcpy); EXPORT(memset);
423 EXPORT(memcmp);
424 //EXPORT(memcpyw);      EXPORT(memsetw);
425 EXPORT(memcpyd);        EXPORT(memsetd);
426 EXPORT(inb);    EXPORT(inw);    EXPORT(ind);
427 EXPORT(outb);   EXPORT(outw);   EXPORT(outd);
428 EXPORT(__udivdi3);      EXPORT(__umoddi3);
429
430 EXPORT(SHORTLOCK);
431 EXPORT(SHORTREL);
432 EXPORT(IS_LOCKED);

UCC git Repository :: git.ucc.asn.au