Lots of work on the AcessNative kernel
[tpg/acess2.git] / Kernel / arch / x86 / lib.c
1 /*
2  * AcessOS Microkernel Version
3  * lib.c
4  */
5 #include <acess.h>
6 #include <threads.h>
7
8 #define TRACE_LOCKS     1
9
10 extern int      GetCPUNum(void);
11
12 // === CODE ===
13 /**
14  * \brief Determine if a short spinlock is locked
15  * \param Lock  Lock pointer
16  */
17 int IS_LOCKED(struct sShortSpinlock *Lock)
18 {
19         return !!Lock->Lock;
20 }
21
22 /**
23  * \brief Check if the current CPU has the lock
24  * \param Lock  Lock pointer
25  */
26 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
27 {
28         #if STACKED_LOCKS == 1
29         return Lock->Lock == GetCPUNum() + 1;
30         #elif STACKED_LOCKS == 2
31         return Lock->Lock == Proc_GetCurThread();
32         #else
33         return 0;
34         #endif
35 }
36
37 /**
38  * \brief Acquire a Short Spinlock
39  * \param Lock  Lock pointer
40  * 
41  * This type of mutex should only be used for very short sections of code,
42  * or in places where a Mutex_* would be overkill, such as appending
43  * an element to linked list (usually two assignement lines in C)
44  * 
45  * \note This type of lock halts interrupts, so ensure that no timing
46  * functions are called while it is held. As a matter of fact, spend as
47  * little time as possible with this lock held
48  * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
49  */
50 void SHORTLOCK(struct sShortSpinlock *Lock)
51 {
52          int    v = 1;
53         #if LOCK_DISABLE_INTS
54          int    IF;
55         #endif
56         #if STACKED_LOCKS == 1
57          int    cpu = GetCPUNum() + 1;
58         #elif STACKED_LOCKS == 2
59         void    *thread = Proc_GetCurThread();
60         #endif
61         
62         #if LOCK_DISABLE_INTS
63         // Save interrupt state
64         __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
65         IF &= 0x200;    // AND out all but the interrupt flag
66         #endif
67         
68         #if STACKED_LOCKS == 1
69         if( Lock->Lock == cpu ) {
70                 Lock->Depth ++;
71                 return ;
72         }
73         #elif STACKED_LOCKS == 2
74         if( Lock->Lock == thread ) {
75                 Lock->Depth ++;
76                 return ;
77         }
78         #endif
79         
80         // Wait for another CPU to release
81         while(v) {
82                 // CMPXCHG:
83                 //  If r/m32 == EAX, set ZF and set r/m32 = r32
84                 //  Else, clear ZF and set EAX = r/m32
85                 #if STACKED_LOCKS == 1
86                 __ASM__("lock cmpxchgl %2, (%3)"
87                         : "=a"(v)
88                         : "a"(0), "r"(cpu), "r"(&Lock->Lock)
89                         );
90                 #elif STACKED_LOCKS == 2
91                 __ASM__("lock cmpxchgl %2, (%3)"
92                         : "=a"(v)
93                         : "a"(0), "r"(thread), "r"(&Lock->Lock)
94                         );
95                 #else
96                 __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(&Lock->Lock));
97                 #endif
98                 
99                 #if LOCK_DISABLE_INTS
100                 if( v ) __ASM__("sti"); // Re-enable interrupts
101                 #endif
102         }
103         
104         #if LOCK_DISABLE_INTS
105         __ASM__("cli");
106         Lock->IF = IF;
107         #endif
108         
109         #if TRACE_LOCKS
110         Log_Log("LOCK", "%p locked by %p\n", Lock, __builtin_return_address(0));
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         Log_Log("LOCK", "%p released by %p\n", Lock, __builtin_return_address(0));
121         #endif
122         
123         #if STACKED_LOCKS
124         if( Lock->Depth ) {
125                 Lock->Depth --;
126                 return ;
127         }
128         #endif
129         
130         #if LOCK_DISABLE_INTS
131         // Lock->IF can change anytime once Lock->Lock is zeroed
132         if(Lock->IF) {
133                 Lock->Lock = 0;
134                 __ASM__ ("sti");
135         }
136         else {
137                 Lock->Lock = 0;
138         }
139         #else
140         Lock->Lock = 0;
141         #endif
142 }
143
144 // === IO Commands ===
145 void outb(Uint16 Port, Uint8 Data)
146 {
147         __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
148 }
149 void outw(Uint16 Port, Uint16 Data)
150 {
151         __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
152 }
153 void outd(Uint16 Port, Uint32 Data)
154 {
155         __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
156 }
157 Uint8 inb(Uint16 Port)
158 {
159         Uint8   ret;
160         __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
161         return ret;
162 }
163 Uint16 inw(Uint16 Port)
164 {
165         Uint16  ret;
166         __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
167         return ret;
168 }
169 Uint32 ind(Uint16 Port)
170 {
171         Uint32  ret;
172         __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
173         return ret;
174 }
175
176 /**
177  * \fn void *memset(void *Dest, int Val, size_t Num)
178  * \brief Do a byte granuality set of Dest
179  */
180 void *memset(void *Dest, int Val, size_t Num)
181 {
182         Uint32  val = Val&0xFF;
183         val |= val << 8;
184         val |= val << 16;
185         __asm__ __volatile__ (
186                 "rep stosl;\n\t"
187                 "mov %3, %%ecx;\n\t"
188                 "rep stosb"
189                 :: "D" (Dest), "a" (val), "c" (Num/4), "r" (Num&3));
190         return Dest;
191 }
192 /**
193  * \brief Set double words
194  */
195 void *memsetd(void *Dest, Uint32 Val, size_t Num)
196 {
197         __asm__ __volatile__ ("rep stosl" :: "D" (Dest), "a" (Val), "c" (Num));
198         return Dest;
199 }
200
201 /**
202  * \fn int memcmp(const void *m1, const void *m2, size_t Num)
203  * \brief Compare two pieces of memory
204  */
205 int memcmp(const void *m1, const void *m2, size_t Num)
206 {
207         if( Num == 0 )  return 0;       // No bytes are always identical
208         
209         while(Num--)
210         {
211                 if(*(Uint8*)m1 != *(Uint8*)m2)
212                         return *(Uint8*)m1 - *(Uint8*)m2;
213                 m1 ++;
214                 m2 ++;
215         }
216         return 0;
217 }
218
219 /**
220  * \fn void *memcpy(void *Dest, const void *Src, size_t Num)
221  * \brief Copy \a Num bytes from \a Src to \a Dest
222  */
223 void *memcpy(void *Dest, const void *Src, size_t Num)
224 {
225         if( ((Uint)Dest & 3) || ((Uint)Src & 3) )
226                 __asm__ __volatile__ ("rep movsb" :: "D" (Dest), "S" (Src), "c" (Num));
227         else {
228                 __asm__ __volatile__ (
229                         "rep movsl;\n\t"
230                         "mov %3, %%ecx;\n\t"
231                         "rep movsb"
232                         :: "D" (Dest), "S" (Src), "c" (Num/4), "r" (Num&3));
233         }
234         return Dest;
235 }
236 /**
237  * \fn void *memcpyd(void *Dest, const void *Src, size_t Num)
238  * \brief Copy \a Num DWORDs from \a Src to \a Dest
239  */
240 void *memcpyd(void *Dest, const void *Src, size_t Num)
241 {
242         __asm__ __volatile__ ("rep movsl" :: "D" (Dest), "S" (Src), "c" (Num));
243         return Dest;
244 }
245
246 /**
247  * \fn Uint64 __udivdi3(Uint64 Num, Uint64 Den)
248  * \brief Divide two 64-bit integers
249  */
250 Uint64 __udivdi3(Uint64 Num, Uint64 Den)
251 {
252         Uint64  P[2];
253         Uint64  q = 0;
254          int    i;
255         
256         if(Den == 0)    __asm__ __volatile__ ("int $0x0");
257         // Common speedups
258         if(Num <= 0xFFFFFFFF && Den <= 0xFFFFFFFF)
259                 return (Uint32)Num / (Uint32)Den;
260         if(Den == 1)    return Num;
261         if(Den == 2)    return Num >> 1;        // Speed Hacks
262         if(Den == 4)    return Num >> 2;        // Speed Hacks
263         if(Den == 8)    return Num >> 3;        // Speed Hacks
264         if(Den == 16)   return Num >> 4;        // Speed Hacks
265         if(Den == 32)   return Num >> 5;        // Speed Hacks
266         if(Den == 1024) return Num >> 10;       // Speed Hacks
267         if(Den == 2048) return Num >> 11;       // Speed Hacks
268         if(Den == 4096) return Num >> 12;
269         if(Num < Den)   return 0;
270         if(Num < Den*2) return 1;
271         if(Num == Den*2)        return 2;
272         
273         // Restoring division, from wikipedia
274         // http://en.wikipedia.org/wiki/Division_(digital)
275         P[0] = Num;     P[1] = 0;
276         for( i = 64; i--; )
277         {
278                 // P <<= 1;
279                 P[1] = (P[1] << 1) | (P[0] >> 63);
280                 P[0] = P[0] << 1;
281                 
282                 // P -= Den << 64
283                 P[1] -= Den;
284                 
285                 // P >= 0
286                 if( !(P[1] & (1ULL<<63)) ) {
287                         q |= (Uint64)1 << (63-i);
288                 }
289                 else {
290                         //q |= 0 << (63-i);
291                         P[1] += Den;
292                 }
293         }
294         
295         return q;
296 }
297
298 /**
299  * \fn Uint64 __umoddi3(Uint64 Num, Uint64 Den)
300  * \brief Get the modulus of two 64-bit integers
301  */
302 Uint64 __umoddi3(Uint64 Num, Uint64 Den)
303 {
304         if(Den == 0)    __asm__ __volatile__ ("int $0x0");      // Call Div by Zero Error
305         if(Den == 1)    return 0;       // Speed Hacks
306         if(Den == 2)    return Num & 1; // Speed Hacks
307         if(Den == 4)    return Num & 3; // Speed Hacks
308         if(Den == 8)    return Num & 7; // Speed Hacks
309         if(Den == 16)   return Num & 15;        // Speed Hacks
310         if(Den == 32)   return Num & 31;        // Speed Hacks
311         if(Den == 1024) return Num & 1023;      // Speed Hacks
312         if(Den == 2048) return Num & 2047;      // Speed Hacks
313         if(Den == 4096) return Num & 4095;      // Speed Hacks
314         
315         if(Num >> 32 == 0 && Den >> 32 == 0)
316                 return (Uint32)Num % (Uint32)Den;
317         
318         return Num - __udivdi3(Num, Den) * Den;
319 }
320
321 Uint16 LittleEndian16(Uint16 Val)
322 {
323         return Val;
324 }
325 Uint16 BigEndian16(Uint16 Val)
326 {
327         return ((Val&0xFF)<<8) | ((Val>>8)&0xFF);
328 }
329 Uint32 LittleEndian32(Uint32 Val)
330 {
331         return Val;
332 }
333 Uint32 BigEndian32(Uint32 Val)
334 {
335         return ((Val&0xFF)<<24) | ((Val&0xFF00)<<8) | ((Val>>8)&0xFF00) | ((Val>>24)&0xFF);
336 }
337
338 // --- EXPORTS ---
339 EXPORT(memcpy); EXPORT(memset);
340 EXPORT(memcmp);
341 //EXPORT(memcpyw);      EXPORT(memsetw);
342 EXPORT(memcpyd);        EXPORT(memsetd);
343 EXPORT(inb);    EXPORT(inw);    EXPORT(ind);
344 EXPORT(outb);   EXPORT(outw);   EXPORT(outd);
345 EXPORT(__udivdi3);      EXPORT(__umoddi3);
346
347 EXPORT(LittleEndian16); EXPORT(BigEndian16);
348 EXPORT(LittleEndian32); EXPORT(BigEndian32);
349
350 EXPORT(SHORTLOCK);
351 EXPORT(SHORTREL);
352 EXPORT(IS_LOCKED);

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