977cffd1ee283d36c687ab951c53d17f824ace95
[tpg/acess2.git] / KernelLand / Kernel / arch / x86_64 / lib.c
1 /*
2  */
3 #include <acess.h>
4 #include <arch.h>
5 #include <drv_serial.h>
6
7 #define DEBUG_TO_E9     1
8 #define DEBUG_TO_SERIAL 1
9 #define SERIAL_PORT     0x3F8
10 #define GDB_SERIAL_PORT 0x2F8
11
12
13 // === IMPORTS ===
14 extern int      GetCPUNum(void);
15 extern void     *Proc_GetCurThread(void);
16
17 // === GLOBALS ===
18  int    gbDebug_SerialSetup = 0;
19  int    gbGDB_SerialSetup = 0;
20
21 // === PROTOTYPEs ===
22  int    putDebugChar(char ch);
23 void    Debug_SerialIRQHandler(int irq, void *unused);
24
25 // === CODE ===
26 /**
27  * \brief Determine if a short spinlock is locked
28  * \param Lock  Lock pointer
29  */
30 int IS_LOCKED(struct sShortSpinlock *Lock)
31 {
32         return !!Lock->Lock;
33 }
34
35 /**
36  * \brief Check if the current CPU has the lock
37  * \param Lock  Lock pointer
38  */
39 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
40 {
41         #if STACKED_LOCKS == 1
42         return Lock->Lock == GetCPUNum() + 1;
43         #elif STACKED_LOCKS == 2
44         return Lock->Lock == Proc_GetCurThread();
45         #else
46         return 0;
47         #endif
48 }
49
50 /**
51  * \brief Acquire a Short Spinlock
52  * \param Lock  Lock pointer
53  * 
54  * This type of mutex should only be used for very short sections of code,
55  * or in places where a Mutex_* would be overkill, such as appending
56  * an element to linked list (usually two assignement lines in C)
57  * 
58  * \note This type of lock halts interrupts, so ensure that no timing
59  * functions are called while it is held. As a matter of fact, spend as
60  * little time as possible with this lock held
61  * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
62  */
63 void SHORTLOCK(struct sShortSpinlock *Lock)
64 {
65          int    v = 1;
66         #if LOCK_DISABLE_INTS
67          int    IF;
68         #endif
69         #if STACKED_LOCKS == 1
70          int    cpu = GetCPUNum() + 1;
71         #elif STACKED_LOCKS == 2
72         void    *thread = Proc_GetCurThread();
73         #endif
74         
75         #if LOCK_DISABLE_INTS
76         // Save interrupt state and clear interrupts
77         __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
78         IF &= 0x200;    // AND out all but the interrupt flag
79         #endif
80         
81         #if STACKED_LOCKS == 1
82         if( Lock->Lock == cpu ) {
83                 Lock->Depth ++;
84                 return ;
85         }
86         #elif STACKED_LOCKS == 2
87         if( Lock->Lock == thread ) {
88                 Lock->Depth ++;
89                 return ;
90         }
91         #endif
92         
93         // Wait for another CPU to release
94         while(v) {
95                 // CMPXCHG:
96                 //  If r/m32 == EAX, set ZF and set r/m32 = r32
97                 //  Else, clear ZF and set EAX = r/m32
98                 #if STACKED_LOCKS == 1
99                 __ASM__("lock cmpxchgl %2, (%3)"
100                         : "=a"(v)
101                         : "a"(0), "r"(cpu), "r"(&Lock->Lock)
102                         );
103                 #elif STACKED_LOCKS == 2
104                 __ASM__("lock cmpxchgq %2, (%3)"
105                         : "=a"(v)
106                         : "a"(0), "r"(thread), "r"(&Lock->Lock)
107                         );
108                 #else
109                 __ASM__("xchgl %0, (%2)":"=a"(v):"a"(1),"D"(&Lock->Lock));
110                 #endif
111                 
112                 #if LOCK_DISABLE_INTS
113                 if( v ) __ASM__("sti"); // Re-enable interrupts
114                 #endif
115         }
116         
117         #if LOCK_DISABLE_INTS
118         __ASM__("cli");
119         Lock->IF = IF;
120         #endif
121 }
122 /**
123  * \brief Release a short lock
124  * \param Lock  Lock pointer
125  */
126 void SHORTREL(struct sShortSpinlock *Lock)
127 {
128         #if STACKED_LOCKS
129         if( Lock->Depth ) {
130                 Lock->Depth --;
131                 return ;
132         }
133         #endif
134         
135         #if LOCK_DISABLE_INTS
136         // Lock->IF can change anytime once Lock->Lock is zeroed
137         if(Lock->IF) {
138                 Lock->Lock = 0;
139                 __ASM__ ("sti");
140         }
141         else {
142                 Lock->Lock = 0;
143         }
144         #else
145         Lock->Lock = 0;
146         #endif
147 }
148
149 void __AtomicTestSetLoop(Uint *Ptr, Uint Value)
150 {
151         __ASM__(
152                 "1:\n\t"
153                 "xor %%eax, %%eax;\n\t"
154                 "lock cmpxchg %0, (%1);\n\t"    // if( Ptr==0 ) { ZF=1; Ptr=Value } else { ZF=0; _=Ptr }
155                 "jnz 1b;\n\t"
156                 :: "r"(Value), "r"(Ptr)
157                 : "eax" // EAX clobbered
158                 );
159 }
160
161 // === DEBUG IO ===
162 #if USE_GDB_STUB
163 void initGdbSerial(void)
164 {
165         outb(GDB_SERIAL_PORT + 1, 0x00);    // Disable all interrupts
166         outb(GDB_SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
167         outb(GDB_SERIAL_PORT + 0, 0x0C);    // Set divisor to 12 (lo byte) 9600 baud
168         outb(GDB_SERIAL_PORT + 1, 0x00);    //  (base is         (hi byte)
169         outb(GDB_SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit (8N1)
170         outb(GDB_SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
171         outb(GDB_SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
172         gbDebug_SerialSetup = 1;
173 }
174 int putDebugChar(char ch)
175 {
176         if(!gbGDB_SerialSetup) {
177                 initGdbSerial();
178         }
179         while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
180         outb(GDB_SERIAL_PORT, ch);
181         return 0;
182 }
183 int getDebugChar(void)
184 {
185         if(!gbGDB_SerialSetup) {
186                 initGdbSerial();
187         }
188         while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0)     ;
189         return inb(GDB_SERIAL_PORT);
190 }
191 #endif
192
193 void Debug_SerialIRQHandler(int irq, void *unused)
194 {
195         if( (inb(SERIAL_PORT+5) & 0x01) == 0 ) {
196                 Debug("Serial no data");
197                 return ;
198         }
199         
200         char ch = inb(SERIAL_PORT);
201         //Debug("Serial RX 0x%x", ch);
202         Serial_ByteReceived(gSerial_KernelDebugPort, ch);
203 }
204
205 void Debug_PutCharDebug(char ch)
206 {
207         #if DEBUG_TO_E9
208         __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
209         #endif
210         
211         #if DEBUG_TO_SERIAL
212         if(!gbDebug_SerialSetup) {
213                 outb(SERIAL_PORT + 1, 0x00);    // Disable all interrupts
214                 outb(SERIAL_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
215                 outb(SERIAL_PORT + 0, 0x0C);    // Set divisor to 12 (lo byte) 9600 baud
216                 outb(SERIAL_PORT + 1, 0x00);    //                   (hi byte)
217                 outb(SERIAL_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
218                 outb(SERIAL_PORT + 2, 0xC7);    // Enable FIFO with 14-byte threshold and clear it
219                 outb(SERIAL_PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
220                 outb(SERIAL_PORT + 1, 0x05);    // Enable ERBFI (Rx Full), ELSI (Line Status)
221                 gbDebug_SerialSetup = 1;
222                 IRQ_AddHandler(4, Debug_SerialIRQHandler, NULL);
223         }
224         while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
225         outb(SERIAL_PORT, ch);
226         #endif
227 }
228
229 void Debug_PutStringDebug(const char *String)
230 {
231         while(*String)
232                 Debug_PutCharDebug(*String++);
233 }
234
235 // === PORT IO ===
236 void outb(Uint16 Port, Uint8 Data)
237 {
238         __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
239 }
240 void outw(Uint16 Port, Uint16 Data)
241 {
242         __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
243 }
244 void outd(Uint16 Port, Uint32 Data)
245 {
246         __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
247 }
248 Uint8 inb(Uint16 Port)
249 {
250         Uint8   ret;
251         __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
252         return ret;
253 }
254 Uint16 inw(Uint16 Port)
255 {
256         Uint16  ret;
257         __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
258         return ret;
259 }
260 Uint32 ind(Uint16 Port)
261 {
262         Uint32  ret;
263         __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
264         return ret;
265 }
266
267 // === Endianness ===
268 /*
269 Uint32 BigEndian32(Uint32 Value)
270 {
271         Uint32  ret;
272         ret = (Value >> 24);
273         ret |= ((Value >> 16) & 0xFF) << 8;
274         ret |= ((Value >>  8) & 0xFF) << 16;
275         ret |= ((Value >>  0) & 0xFF) << 24;
276         return ret;
277 }
278
279 Uint16 BigEndian16(Uint16 Value)
280 {
281         return  (Value>>8)|(Value<<8);
282 }
283 */
284
285 // === Memory Manipulation ===
286 int memcmp(const void *__dest, const void *__src, size_t __count)
287 {
288         if( ((tVAddr)__dest & 7) != ((tVAddr)__src & 7) ) {
289                 const Uint8     *src = __src, *dst = __dest;
290                 while(__count)
291                 {
292                         if( *src != *dst )
293                                 return *dst - *src;
294                         src ++; dst ++; __count --;
295                 }
296                 return 0;
297         }
298         else {
299                 const Uint8     *src = __src;
300                 const Uint8     *dst = __dest;
301                 const Uint64    *src64, *dst64;
302                 
303                 while( (tVAddr)src & 7 && __count ) {
304                         if( *src != *dst )
305                                 return *dst - *src;
306                         dst ++; src ++; __count --;
307                 }
308
309                 src64 = (void*)src;
310                 dst64 = (void*)dst;
311
312                 while( __count >= 8 )
313                 {
314                         if( *src64 != *dst64 )
315                         {
316                                 src = (void*)src64;
317                                 dst = (void*)dst64;
318                                 if(src[0] != dst[0])    return dst[0]-src[0];
319                                 if(src[1] != dst[1])    return dst[1]-src[1];
320                                 if(src[2] != dst[2])    return dst[2]-src[2];
321                                 if(src[3] != dst[3])    return dst[3]-src[3];
322                                 if(src[4] != dst[4])    return dst[4]-src[4];
323                                 if(src[5] != dst[5])    return dst[5]-src[5];
324                                 if(src[6] != dst[6])    return dst[6]-src[6];
325                                 if(src[7] != dst[7])    return dst[7]-src[7];
326                                 return -1;      // This should never happen
327                         }
328                         __count -= 8;
329                         src64 ++;
330                         dst64 ++;
331                 }
332
333                 src = (void*)src64;
334                 dst = (void*)dst64;
335                 while( __count-- )
336                 {
337                         if(*dst != *src)        return *dst - *src;
338                         dst ++;
339                         src ++;
340                 }
341         }
342         return 0;
343 }
344
345 void *memcpy(void *__dest, const void *__src, size_t __count)
346 {
347         tVAddr  dst = (tVAddr)__dest, src = (tVAddr)__src;
348         if( (dst & 7) != (src & 7) )
349         {
350                 __asm__ __volatile__ ("rep movsb" : : "D"(dst),"S"(src),"c"(__count));
351         }
352         else
353         {
354                 while( (src & 7) && __count ) {
355                         *(char*)dst++ = *(char*)src++;
356                         __count --;
357                 }
358
359                 __asm__ __volatile__ ("rep movsq" : "=D"(dst),"=S"(src) : "0"(dst),"1"(src),"c"(__count/8));
360                 __count = __count & 7;
361                 while( __count-- )
362                         *(char*)dst++ = *(char*)src++;
363         }
364         return __dest;
365 }
366
367 void *memset(void *__dest, int __val, size_t __count)
368 {
369         if( __val != 0 || ((tVAddr)__dest & 7) != 0 )
370                 __asm__ __volatile__ ("rep stosb" : : "D"(__dest),"a"(__val),"c"(__count));
371         else {
372                 Uint8   *dst = __dest;
373
374                 __asm__ __volatile__ ("rep stosq" : : "D"(dst),"a"(0),"c"(__count/8));
375                 dst += __count & ~7;
376                 __count = __count & 7;
377                 while( __count-- )
378                         *dst++ = 0;
379         }
380         return __dest;
381 }
382
383 void *memsetd(void *__dest, Uint32 __val, size_t __count)
384 {
385         __asm__ __volatile__ ("rep stosl" : : "D"(__dest),"a"(__val),"c"(__count));
386         return __dest;
387 }
388
389 Uint64 DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem)
390 {
391         Uint64  ret, rem;
392         __asm__ __volatile__(
393                 "div %4"
394                 : "=a" (ret), "=d" (rem)
395                 : "a" ( Num ), "d" (0), "r" (Den)
396                 );
397         if(Rem) *Rem = rem;
398         return ret;
399 }
400
401 EXPORT(memcpy);
402 EXPORT(memset);
403

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