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

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