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);
26 * \brief Determine if a short spinlock is locked
27 * \param Lock Lock pointer
29 int IS_LOCKED(struct sShortSpinlock *Lock)
35 * \brief Check if the current CPU has the lock
36 * \param Lock Lock pointer
38 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
40 #if STACKED_LOCKS == 1
41 return Lock->Lock == GetCPUNum() + 1;
42 #elif STACKED_LOCKS == 2
43 return Lock->Lock == Proc_GetCurThread();
50 * \brief Acquire a Short Spinlock
51 * \param Lock Lock pointer
53 * This type of mutex should only be used for very short sections of code,
54 * or in places where a Mutex_* would be overkill, such as appending
55 * an element to linked list (usually two assignement lines in C)
57 * \note This type of lock halts interrupts, so ensure that no timing
58 * functions are called while it is held. As a matter of fact, spend as
59 * little time as possible with this lock held
60 * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
62 void SHORTLOCK(struct sShortSpinlock *Lock)
68 #if STACKED_LOCKS == 1
69 int cpu = GetCPUNum() + 1;
70 #elif STACKED_LOCKS == 2
71 void *thread = Proc_GetCurThread();
75 // Save interrupt state and clear interrupts
76 __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
77 IF &= 0x200; // AND out all but the interrupt flag
80 #if STACKED_LOCKS == 1
81 if( Lock->Lock == cpu ) {
85 #elif STACKED_LOCKS == 2
86 if( Lock->Lock == thread ) {
92 // Wait for another CPU to release
95 // If r/m32 == EAX, set ZF and set r/m32 = r32
96 // Else, clear ZF and set EAX = r/m32
97 #if STACKED_LOCKS == 1
98 __ASM__("lock cmpxchgl %2, (%3)"
100 : "a"(0), "r"(cpu), "r"(&Lock->Lock)
102 #elif STACKED_LOCKS == 2
103 __ASM__("lock cmpxchgq %2, (%3)"
105 : "a"(0), "r"(thread), "r"(&Lock->Lock)
108 __ASM__("xchgl %0, (%2)":"=a"(v):"a"(1),"D"(&Lock->Lock));
111 #if LOCK_DISABLE_INTS
112 if( v ) __ASM__("sti"); // Re-enable interrupts
116 #if LOCK_DISABLE_INTS
122 * \brief Release a short lock
123 * \param Lock Lock pointer
125 void SHORTREL(struct sShortSpinlock *Lock)
134 #if LOCK_DISABLE_INTS
135 // Lock->IF can change anytime once Lock->Lock is zeroed
150 void initGdbSerial(void)
152 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
153 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
154 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
155 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
156 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
157 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
158 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
159 gbDebug_SerialSetup = 1;
161 int putDebugChar(char ch)
163 if(!gbGDB_SerialSetup) {
166 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
167 outb(GDB_SERIAL_PORT, ch);
170 int getDebugChar(void)
172 if(!gbGDB_SerialSetup) {
175 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
176 return inb(GDB_SERIAL_PORT);
180 void Debug_SerialIRQHandler(int irq, void *unused)
182 if( (inb(SERIAL_PORT+5) & 0x01) == 0 ) {
183 Debug("Serial no data");
187 char ch = inb(SERIAL_PORT);
188 //Debug("Serial RX 0x%x", ch);
189 Serial_ByteReceived(gSerial_KernelDebugPort, ch);
192 void Debug_PutCharDebug(char ch)
195 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
199 if(!gbDebug_SerialSetup) {
200 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
201 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
202 outb(SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
203 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
204 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
205 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
206 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
207 outb(SERIAL_PORT + 1, 0x05); // Enable ERBFI (Rx Full), ELSI (Line Status)
208 gbDebug_SerialSetup = 1;
209 IRQ_AddHandler(4, Debug_SerialIRQHandler, NULL);
211 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
212 outb(SERIAL_PORT, ch);
216 void Debug_PutStringDebug(const char *String)
219 Debug_PutCharDebug(*String++);
223 void outb(Uint16 Port, Uint8 Data)
225 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
227 void outw(Uint16 Port, Uint16 Data)
229 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
231 void outd(Uint16 Port, Uint32 Data)
233 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
235 Uint8 inb(Uint16 Port)
238 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
241 Uint16 inw(Uint16 Port)
244 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
247 Uint32 ind(Uint16 Port)
250 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
254 // === Endianness ===
256 Uint32 BigEndian32(Uint32 Value)
260 ret |= ((Value >> 16) & 0xFF) << 8;
261 ret |= ((Value >> 8) & 0xFF) << 16;
262 ret |= ((Value >> 0) & 0xFF) << 24;
266 Uint16 BigEndian16(Uint16 Value)
268 return (Value>>8)|(Value<<8);
272 // === Memory Manipulation ===
273 int memcmp(const void *__dest, const void *__src, size_t __count)
275 if( ((tVAddr)__dest & 7) != ((tVAddr)__src & 7) ) {
276 const Uint8 *src = __src, *dst = __dest;
281 src ++; dst ++; __count --;
286 const Uint8 *src = __src;
287 const Uint8 *dst = __dest;
288 const Uint64 *src64, *dst64;
290 while( (tVAddr)src & 7 && __count ) {
293 dst ++; src ++; __count --;
299 while( __count >= 8 )
301 if( *src64 != *dst64 )
305 if(src[0] != dst[0]) return dst[0]-src[0];
306 if(src[1] != dst[1]) return dst[1]-src[1];
307 if(src[2] != dst[2]) return dst[2]-src[2];
308 if(src[3] != dst[3]) return dst[3]-src[3];
309 if(src[4] != dst[4]) return dst[4]-src[4];
310 if(src[5] != dst[5]) return dst[5]-src[5];
311 if(src[6] != dst[6]) return dst[6]-src[6];
312 if(src[7] != dst[7]) return dst[7]-src[7];
313 return -1; // This should never happen
324 if(*dst != *src) return *dst - *src;
332 void *memcpy(void *__dest, const void *__src, size_t __count)
334 tVAddr dst = (tVAddr)__dest, src = (tVAddr)__src;
335 if( (dst & 7) != (src & 7) )
337 __asm__ __volatile__ ("rep movsb" : : "D"(dst),"S"(src),"c"(__count));
341 while( (src & 7) && __count ) {
342 *(char*)dst++ = *(char*)src++;
346 __asm__ __volatile__ ("rep movsq" : "=D"(dst),"=S"(src) : "0"(dst),"1"(src),"c"(__count/8));
347 __count = __count & 7;
349 *(char*)dst++ = *(char*)src++;
354 void *memset(void *__dest, int __val, size_t __count)
356 if( __val != 0 || ((tVAddr)__dest & 7) != 0 )
357 __asm__ __volatile__ ("rep stosb" : : "D"(__dest),"a"(__val),"c"(__count));
361 __asm__ __volatile__ ("rep stosq" : : "D"(dst),"a"(0),"c"(__count/8));
363 __count = __count & 7;
370 void *memsetd(void *__dest, Uint32 __val, size_t __count)
372 __asm__ __volatile__ ("rep stosl" : : "D"(__dest),"a"(__val),"c"(__count));
376 Uint64 DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem)
379 __asm__ __volatile__(
381 : "=a" (ret), "=d" (rem)
382 : "a" ( Num ), "d" (0), "r" (Den)