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

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