5 #include <drv_serial.h>
8 #define DEBUG_TO_SERIAL 1
9 #define SERIAL_PORT 0x3F8
10 #define GDB_SERIAL_PORT 0x2F8
14 extern int GetCPUNum(void);
15 extern void *Proc_GetCurThread(void);
18 int gbDebug_SerialSetup = 0;
19 int gbGDB_SerialSetup = 0;
22 int putDebugChar(char ch);
23 void Debug_SerialIRQHandler(int irq, void *unused);
27 * \brief Determine if a short spinlock is locked
28 * \param Lock Lock pointer
30 int IS_LOCKED(struct sShortSpinlock *Lock)
36 * \brief Check if the current CPU has the lock
37 * \param Lock Lock pointer
39 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
41 #if STACKED_LOCKS == 1
42 return Lock->Lock == GetCPUNum() + 1;
43 #elif STACKED_LOCKS == 2
44 return Lock->Lock == Proc_GetCurThread();
51 * \brief Acquire a Short Spinlock
52 * \param Lock Lock pointer
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)
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
63 void SHORTLOCK(struct sShortSpinlock *Lock)
69 #if STACKED_LOCKS == 1
70 int cpu = GetCPUNum() + 1;
71 #elif STACKED_LOCKS == 2
72 void *thread = Proc_GetCurThread();
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
81 #if STACKED_LOCKS == 1
82 if( Lock->Lock == cpu ) {
86 #elif STACKED_LOCKS == 2
87 if( Lock->Lock == thread ) {
93 // Wait for another CPU to release
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)"
101 : "a"(0), "r"(cpu), "r"(&Lock->Lock)
103 #elif STACKED_LOCKS == 2
104 __ASM__("lock cmpxchgq %2, (%3)"
106 : "a"(0), "r"(thread), "r"(&Lock->Lock)
109 __ASM__("xchgl %0, (%2)":"=a"(v):"a"(1),"D"(&Lock->Lock));
112 #if LOCK_DISABLE_INTS
113 if( v ) __ASM__("sti"); // Re-enable interrupts
117 #if LOCK_DISABLE_INTS
123 * \brief Release a short lock
124 * \param Lock Lock pointer
126 void SHORTREL(struct sShortSpinlock *Lock)
135 #if LOCK_DISABLE_INTS
136 // Lock->IF can change anytime once Lock->Lock is zeroed
151 void initGdbSerial(void)
153 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
154 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
155 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
156 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
157 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
158 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
159 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
160 gbDebug_SerialSetup = 1;
162 int putDebugChar(char ch)
164 if(!gbGDB_SerialSetup) {
167 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
168 outb(GDB_SERIAL_PORT, ch);
171 int getDebugChar(void)
173 if(!gbGDB_SerialSetup) {
176 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
177 return inb(GDB_SERIAL_PORT);
181 void Debug_SerialIRQHandler(int irq, void *unused)
183 if( (inb(SERIAL_PORT+5) & 0x01) == 0 ) {
184 Debug("Serial no data");
188 char ch = inb(SERIAL_PORT);
189 //Debug("Serial RX 0x%x", ch);
190 Serial_ByteReceived(gSerial_KernelDebugPort, ch);
193 void Debug_PutCharDebug(char ch)
196 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
200 if(!gbDebug_SerialSetup) {
201 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
202 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
203 outb(SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
204 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
205 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
206 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
207 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
208 outb(SERIAL_PORT + 1, 0x05); // Enable ERBFI (Rx Full), ELSI (Line Status)
209 gbDebug_SerialSetup = 1;
210 IRQ_AddHandler(4, Debug_SerialIRQHandler, NULL);
212 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
213 outb(SERIAL_PORT, ch);
217 void Debug_PutStringDebug(const char *String)
220 Debug_PutCharDebug(*String++);
224 void outb(Uint16 Port, Uint8 Data)
226 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
228 void outw(Uint16 Port, Uint16 Data)
230 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
232 void outd(Uint16 Port, Uint32 Data)
234 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
236 Uint8 inb(Uint16 Port)
239 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
242 Uint16 inw(Uint16 Port)
245 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
248 Uint32 ind(Uint16 Port)
251 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
255 // === Endianness ===
257 Uint32 BigEndian32(Uint32 Value)
261 ret |= ((Value >> 16) & 0xFF) << 8;
262 ret |= ((Value >> 8) & 0xFF) << 16;
263 ret |= ((Value >> 0) & 0xFF) << 24;
267 Uint16 BigEndian16(Uint16 Value)
269 return (Value>>8)|(Value<<8);
273 // === Memory Manipulation ===
274 int memcmp(const void *__dest, const void *__src, size_t __count)
276 if( ((tVAddr)__dest & 7) != ((tVAddr)__src & 7) ) {
277 const Uint8 *src = __src, *dst = __dest;
282 src ++; dst ++; __count --;
287 const Uint8 *src = __src;
288 const Uint8 *dst = __dest;
289 const Uint64 *src64, *dst64;
291 while( (tVAddr)src & 7 && __count ) {
294 dst ++; src ++; __count --;
300 while( __count >= 8 )
302 if( *src64 != *dst64 )
306 if(src[0] != dst[0]) return dst[0]-src[0];
307 if(src[1] != dst[1]) return dst[1]-src[1];
308 if(src[2] != dst[2]) return dst[2]-src[2];
309 if(src[3] != dst[3]) return dst[3]-src[3];
310 if(src[4] != dst[4]) return dst[4]-src[4];
311 if(src[5] != dst[5]) return dst[5]-src[5];
312 if(src[6] != dst[6]) return dst[6]-src[6];
313 if(src[7] != dst[7]) return dst[7]-src[7];
314 return -1; // This should never happen
325 if(*dst != *src) return *dst - *src;
333 void *memcpy(void *__dest, const void *__src, size_t __count)
335 tVAddr dst = (tVAddr)__dest, src = (tVAddr)__src;
336 if( (dst & 7) != (src & 7) )
338 __asm__ __volatile__ ("rep movsb" : : "D"(dst),"S"(src),"c"(__count));
342 while( (src & 7) && __count ) {
343 *(char*)dst++ = *(char*)src++;
347 __asm__ __volatile__ ("rep movsq" : "=D"(dst),"=S"(src) : "0"(dst),"1"(src),"c"(__count/8));
348 __count = __count & 7;
350 *(char*)dst++ = *(char*)src++;
355 void *memset(void *__dest, int __val, size_t __count)
357 if( __val != 0 || ((tVAddr)__dest & 7) != 0 )
358 __asm__ __volatile__ ("rep stosb" : : "D"(__dest),"a"(__val),"c"(__count));
362 __asm__ __volatile__ ("rep stosq" : : "D"(dst),"a"(0),"c"(__count/8));
364 __count = __count & 7;
371 void *memsetd(void *__dest, Uint32 __val, size_t __count)
373 __asm__ __volatile__ ("rep stosl" : : "D"(__dest),"a"(__val),"c"(__count));
377 Uint64 DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem)
380 __asm__ __volatile__(
382 : "=a" (ret), "=d" (rem)
383 : "a" ( Num ), "d" (0), "r" (Den)