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

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