7 #define DEBUG_TO_SERIAL 1
8 #define SERIAL_PORT 0x3F8
9 #define GDB_SERIAL_PORT 0x2F8
12 extern int GetCPUNum(void);
13 extern void *Proc_GetCurThread(void);
16 int gbDebug_SerialSetup = 0;
17 int gbGDB_SerialSetup = 0;
21 * \brief Determine if a short spinlock is locked
22 * \param Lock Lock pointer
24 int IS_LOCKED(struct sShortSpinlock *Lock)
30 * \brief Check if the current CPU has the lock
31 * \param Lock Lock pointer
33 int CPU_HAS_LOCK(struct sShortSpinlock *Lock)
35 #if STACKED_LOCKS == 1
36 return Lock->Lock == GetCPUNum() + 1;
37 #elif STACKED_LOCKS == 2
38 return Lock->Lock == Proc_GetCurThread();
45 * \brief Acquire a Short Spinlock
46 * \param Lock Lock pointer
48 * This type of mutex should only be used for very short sections of code,
49 * or in places where a Mutex_* would be overkill, such as appending
50 * an element to linked list (usually two assignement lines in C)
52 * \note This type of lock halts interrupts, so ensure that no timing
53 * functions are called while it is held. As a matter of fact, spend as
54 * little time as possible with this lock held
55 * \note If \a STACKED_LOCKS is set, this type of spinlock can be nested
57 void SHORTLOCK(struct sShortSpinlock *Lock)
63 #if STACKED_LOCKS == 1
64 int cpu = GetCPUNum() + 1;
65 #elif STACKED_LOCKS == 2
66 void *thread = Proc_GetCurThread();
70 // Save interrupt state and clear interrupts
71 __ASM__ ("pushf;\n\tpop %0" : "=r"(IF));
72 IF &= 0x200; // AND out all but the interrupt flag
75 #if STACKED_LOCKS == 1
76 if( Lock->Lock == cpu ) {
80 #elif STACKED_LOCKS == 2
81 if( Lock->Lock == thread ) {
87 // Wait for another CPU to release
90 // If r/m32 == EAX, set ZF and set r/m32 = r32
91 // Else, clear ZF and set EAX = r/m32
92 #if STACKED_LOCKS == 1
93 __ASM__("lock cmpxchgl %2, (%3)"
95 : "a"(0), "r"(cpu), "r"(&Lock->Lock)
97 #elif STACKED_LOCKS == 2
98 __ASM__("lock cmpxchgq %2, (%3)"
100 : "a"(0), "r"(thread), "r"(&Lock->Lock)
103 __ASM__("xchgl %0, (%2)":"=a"(v):"a"(1),"D"(&Lock->Lock));
106 #if LOCK_DISABLE_INTS
107 if( v ) __ASM__("sti"); // Re-enable interrupts
111 #if LOCK_DISABLE_INTS
117 * \brief Release a short lock
118 * \param Lock Lock pointer
120 void SHORTREL(struct sShortSpinlock *Lock)
129 #if LOCK_DISABLE_INTS
130 // Lock->IF can change anytime once Lock->Lock is zeroed
144 int putDebugChar(char ch)
146 if(!gbGDB_SerialSetup) {
147 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
148 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
149 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
150 outb(GDB_SERIAL_PORT + 1, 0x00); // (base is (hi byte)
151 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit (8N1)
152 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
153 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
154 gbDebug_SerialSetup = 1;
156 while( (inb(GDB_SERIAL_PORT + 5) & 0x20) == 0 );
157 outb(GDB_SERIAL_PORT, ch);
160 int getDebugChar(void)
162 if(!gbGDB_SerialSetup) {
163 outb(GDB_SERIAL_PORT + 1, 0x00); // Disable all interrupts
164 outb(GDB_SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
165 outb(GDB_SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
166 outb(GDB_SERIAL_PORT + 1, 0x00); // (hi byte)
167 outb(GDB_SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
168 outb(GDB_SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
169 outb(GDB_SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
170 gbDebug_SerialSetup = 1;
172 while( (inb(GDB_SERIAL_PORT + 5) & 1) == 0) ;
173 return inb(GDB_SERIAL_PORT);
176 void Debug_PutCharDebug(char ch)
179 __asm__ __volatile__ ( "outb %%al, $0xe9" :: "a"(((Uint8)ch)) );
183 if(!gbDebug_SerialSetup) {
184 outb(SERIAL_PORT + 1, 0x00); // Disable all interrupts
185 outb(SERIAL_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
186 outb(SERIAL_PORT + 0, 0x0C); // Set divisor to 12 (lo byte) 9600 baud
187 outb(SERIAL_PORT + 1, 0x00); // (hi byte)
188 outb(SERIAL_PORT + 3, 0x03); // 8 bits, no parity, one stop bit
189 outb(SERIAL_PORT + 2, 0xC7); // Enable FIFO with 14-byte threshold and clear it
190 outb(SERIAL_PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
191 gbDebug_SerialSetup = 1;
193 while( (inb(SERIAL_PORT + 5) & 0x20) == 0 );
194 outb(SERIAL_PORT, ch);
199 void outb(Uint16 Port, Uint8 Data)
201 __asm__ __volatile__ ("outb %%al, %%dx"::"d"(Port),"a"(Data));
203 void outw(Uint16 Port, Uint16 Data)
205 __asm__ __volatile__ ("outw %%ax, %%dx"::"d"(Port),"a"(Data));
207 void outd(Uint16 Port, Uint32 Data)
209 __asm__ __volatile__ ("outl %%eax, %%dx"::"d"(Port),"a"(Data));
211 Uint8 inb(Uint16 Port)
214 __asm__ __volatile__ ("inb %%dx, %%al":"=a"(ret):"d"(Port));
217 Uint16 inw(Uint16 Port)
220 __asm__ __volatile__ ("inw %%dx, %%ax":"=a"(ret):"d"(Port));
223 Uint32 ind(Uint16 Port)
226 __asm__ __volatile__ ("inl %%dx, %%eax":"=a"(ret):"d"(Port));
230 // === Endianness ===
231 Uint32 BigEndian32(Uint32 Value)
235 ret |= ((Value >> 16) & 0xFF) << 8;
236 ret |= ((Value >> 8) & 0xFF) << 16;
237 ret |= ((Value >> 0) & 0xFF) << 24;
241 Uint16 BigEndian16(Uint16 Value)
243 return (Value>>8)|(Value<<8);
246 // === Memory Manipulation ===
247 int memcmp(const void *__dest, const void *__src, size_t __count)
249 if( ((tVAddr)__dest & 7) != ((tVAddr)__src & 7) ) {
250 const Uint8 *src = __src, *dst = __dest;
255 src ++; dst ++; __count --;
260 const Uint8 *src = __src;
261 const Uint8 *dst = __dest;
262 const Uint64 *src64, *dst64;
264 while( (tVAddr)src & 7 && __count ) {
267 dst ++; src ++; __count --;
273 while( __count >= 8 )
275 if( *src64 != *dst64 )
279 if(src[0] != dst[0]) return dst[0]-src[0];
280 if(src[1] != dst[1]) return dst[1]-src[1];
281 if(src[2] != dst[2]) return dst[2]-src[2];
282 if(src[3] != dst[3]) return dst[3]-src[3];
283 if(src[4] != dst[4]) return dst[4]-src[4];
284 if(src[5] != dst[5]) return dst[5]-src[5];
285 if(src[6] != dst[6]) return dst[6]-src[6];
286 if(src[7] != dst[7]) return dst[7]-src[7];
287 return -1; // This should never happen
298 if(*dst != *src) return *dst - *src;
306 void *memcpy(void *__dest, const void *__src, size_t __count)
308 if( ((tVAddr)__dest & 7) != ((tVAddr)__src & 7) )
309 __asm__ __volatile__ ("rep movsb" : : "D"(__dest),"S"(__src),"c"(__count));
311 const Uint8 *src = __src;
313 while( (tVAddr)src & 7 && __count ) {
318 __asm__ __volatile__ ("rep movsq" : : "D"(dst),"S"(src),"c"(__count/8));
321 __count = __count & 7;
328 void *memset(void *__dest, int __val, size_t __count)
330 if( __val != 0 || ((tVAddr)__dest & 7) != 0 )
331 __asm__ __volatile__ ("rep stosb" : : "D"(__dest),"a"(__val),"c"(__count));
335 __asm__ __volatile__ ("rep stosq" : : "D"(dst),"a"(0),"c"(__count/8));
337 __count = __count & 7;
344 void *memsetd(void *__dest, Uint32 __val, size_t __count)
346 __asm__ __volatile__ ("rep stosl" : : "D"(__dest),"a"(__val),"c"(__count));