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

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