Fixing commenting (always nice)
[tpg/acess2.git] / Kernel / arch / x86 / include / arch.h
1 /*
2  * Acess2
3  * - x86 Architecture
4  * arch/i386/include/arch.h
5  */
6 #ifndef _ARCH_H_
7 #define _ARCH_H_
8
9 // - Base Defintions
10 #define KERNEL_BASE     0xC0000000
11 #define BITS    32
12
13 // - Processor/Machine Specific Features
14 #if ARCH != i386 && ARCH != i486 && ARCH != i586
15 # error "Unknown architecture '" #ARCH "'"
16 #endif
17
18 #if USE_MP
19 # define        MAX_CPUS        8
20 #else
21 # define        MAX_CPUS        1
22 #endif
23
24 #if USE_PAE
25 # define        PHYS_BITS       48
26 #else
27 # define        PHYS_BITS       32
28 #endif
29
30 #define __ASM__ __asm__ __volatile__
31
32 // === Spinlocks ===
33 /**
34  * \brief Short Spinlock structure
35  */
36 struct sShortSpinlock {
37         volatile int    Lock;   //!< Lock value
38          int    IF;     //!< Interrupt state on call to SHORTLOCK
39 };
40 /**
41  * \brief Determine if a short spinlock is locked
42  * \param Lock  Lock pointer
43  */
44 static inline int IS_LOCKED(struct sShortSpinlock *Lock) {
45         return !!Lock->Lock;
46 }
47 /**
48  * \brief Acquire a Short Spinlock
49  * \param Lock  Lock pointer
50  * 
51  * This type of mutex should only be used for very short sections of code,
52  * or in places where a Mutex_* would be overkill, such as appending
53  * an element to linked list (usually two assignement lines in C)
54  * 
55  * \note This type of lock halts interrupts, so ensure that no timing
56  * functions are called while it is held.
57  */
58 static inline void SHORTLOCK(struct sShortSpinlock *Lock) {
59          int    v = 1;
60         
61         // Save interrupt state
62         __ASM__ ("pushf;\n\tpop %%eax" : "=a"(Lock->IF));
63         Lock->IF &= 0x200;
64         
65         // Stop interrupts
66         __ASM__ ("cli");
67         
68         // Wait for another CPU to release
69         while(v)
70                 __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(&Lock->Lock));
71 }
72 /**
73  * \brief Release a short lock
74  * \param Lock  Lock pointer
75  */
76 static inline void SHORTREL(struct sShortSpinlock *Lock) {
77         Lock->Lock = 0;
78         #if 0   // Which is faster?, meh the test is simpler
79         __ASM__ ("pushf;\n\tor %0, (%%esp);\n\tpopf" : : "a"(Lock->IF));
80         #else
81         if(Lock->IF)    __ASM__ ("sti");
82         #endif
83 }
84
85 // === MACROS ===
86 /**
87  * \brief Halt the CPU
88  */
89 #define HALT()  __asm__ __volatile__ ("hlt")
90 /**
91  * \brief Fire a magic breakpoint (bochs)
92  */
93 #define MAGIC_BREAK()   __asm__ __volatile__ ("xchg %bx, %bx")
94
95 // === TYPES ===
96 typedef unsigned int    Uint;   // Unsigned machine native integer
97 typedef unsigned char   Uint8;
98 typedef unsigned short  Uint16;
99 typedef unsigned long   Uint32;
100 typedef unsigned long long      Uint64;
101 typedef signed int              Sint;   // Signed Machine Native integer
102 typedef signed char             Sint8;
103 typedef signed short    Sint16;
104 typedef signed long             Sint32;
105 typedef signed long long        Sint64;
106 typedef Uint    size_t;
107
108 typedef Uint64  tPAddr;
109 typedef Uint32  tVAddr;
110
111 typedef struct {
112     Uint        gs, fs, es, ds;
113     Uint        edi, esi, ebp, kesp;
114         Uint    ebx, edx, ecx, eax;
115     Uint        int_num, err_code;
116     Uint        eip, cs;
117         Uint    eflags, esp, ss;
118 } tRegs;
119
120 typedef struct {
121         Uint    Resvd1[4];      // GS, FS, ES, DS
122         Uint    Arg4, Arg5;     // EDI, ESI
123         Uint    Arg6;   // EBP
124         Uint    Resvd2[1];      // Kernel ESP
125         union {
126                 Uint    Arg1;
127                 Uint    Error;
128         };      // EBX
129         union {
130                 Uint    Arg3;
131                 Uint    RetHi;  // High 32 bits of ret
132         };      // EDX
133         Uint    Arg2;   // ECX
134         union {
135                 Uint    Num;
136                 Uint    Return;
137         };      // EAX
138         Uint    Resvd3[5];      // Int, Err, Eip, CS, ...
139         Uint    StackPointer;   // ESP
140         Uint    Resvd4[1];      // SS
141 } tSyscallRegs;
142
143 typedef struct {
144         #if USE_PAE
145         Uint    PDPT[4];
146         #else
147         Uint    CR3;
148         #endif
149 } tMemoryState;
150
151 typedef struct {
152         Uint    EIP, ESP, EBP;
153 } tTaskState;
154
155 #endif  // !defined(_ARCH_H_)

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