4 * arch/i386/include/arch.h
10 #define KERNEL_BASE 0xC0000000
13 // Allow nested spinlocks?
14 #define STACKED_LOCKS 1
16 // - Processor/Machine Specific Features
17 #if ARCH != i386 && ARCH != i486 && ARCH != i586
18 # error "Unknown architecture '" #ARCH "'"
33 #define __ASM__ __asm__ __volatile__
37 * \brief Short Spinlock structure
39 struct sShortSpinlock {
40 volatile int Lock; //!< Lock value
41 int IF; //!< Interrupt state on call to SHORTLOCK
47 * \brief Determine if a short spinlock is locked
48 * \param Lock Lock pointer
50 static inline int IS_LOCKED(struct sShortSpinlock *Lock) {
54 * \brief Acquire a Short Spinlock
55 * \param Lock Lock pointer
57 * This type of mutex should only be used for very short sections of code,
58 * or in places where a Mutex_* would be overkill, such as appending
59 * an element to linked list (usually two assignement lines in C)
61 * \note This type of lock halts interrupts, so ensure that no timing
62 * functions are called while it is held. As a matter of fact, spend as
63 * little time as possible with this lock held
65 static inline void SHORTLOCK(struct sShortSpinlock *Lock) {
69 extern int GetCPUNum(void);
70 int cpu = GetCPUNum() + 1;
73 // Save interrupt state and clear interrupts
74 __ASM__ ("pushf;\n\tpop %%eax\n\tcli" : "=a"(IF));
75 IF &= 0x200; // AND out all but the interrupt flag
78 if( Lock->Lock == cpu ) {
84 // Wait for another CPU to release
88 // If r/m32 == EAX, set ZF and set r/m32 = r32
89 // Else, clear ZF and set EAX = r/m32
90 __ASM__("lock cmpxchgl %%ecx, (%%edi)"
92 : "a"(0), "c"(cpu), "D"(&Lock->Lock)
95 __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(&Lock->Lock));
102 * \brief Release a short lock
103 * \param Lock Lock pointer
105 static inline void SHORTREL(struct sShortSpinlock *Lock) {
112 // Lock->IF can change anytime once Lock->Lock is zeroed
124 * \brief Halt the CPU
126 #define HALT() __asm__ __volatile__ ("hlt")
128 * \brief Fire a magic breakpoint (bochs)
130 #define MAGIC_BREAK() __asm__ __volatile__ ("xchg %bx, %bx")
133 typedef unsigned int Uint; // Unsigned machine native integer
134 typedef unsigned char Uint8;
135 typedef unsigned short Uint16;
136 typedef unsigned long Uint32;
137 typedef unsigned long long Uint64;
138 typedef signed int Sint; // Signed Machine Native integer
139 typedef signed char Sint8;
140 typedef signed short Sint16;
141 typedef signed long Sint32;
142 typedef signed long long Sint64;
145 typedef Uint64 tPAddr;
146 typedef Uint32 tVAddr;
150 Uint edi, esi, ebp, kesp;
151 Uint ebx, edx, ecx, eax;
152 Uint int_num, err_code;
154 Uint eflags, esp, ss;
158 Uint Resvd1[4]; // GS, FS, ES, DS
159 Uint Arg4, Arg5; // EDI, ESI
161 Uint Resvd2[1]; // Kernel ESP
168 Uint RetHi; // High 32 bits of ret
175 Uint Resvd3[5]; // Int, Err, Eip, CS, ...
176 Uint StackPointer; // ESP
177 Uint Resvd4[1]; // SS
192 #endif // !defined(_ARCH_H_)