Fixing to get x86_64 build to compile after threading overhaul
[tpg/acess2.git] / Kernel / arch / x86_64 / include / arch.h
1 /*
2  * Acess2 x86-64 Architecure Module
3  * - By John Hodge (thePowersGang)
4  */
5 #ifndef _ARCH_H_
6 #define _ARCH_H_
7
8 //#include <stdint.h>
9 //#define KERNEL_BASE   0xFFFF8000##00000000
10 #define KERNEL_BASE     0xFFFFFFFF##80000000
11 #define BITS    64
12
13 //#define INT_MAX       0x7FFFFFFF
14 //#define UINT_MAX      0xFFFFFFFF
15
16 // === Core Types ===
17 typedef signed char     Sint8;
18 typedef unsigned char   Uint8;
19 typedef signed short    Sint16;
20 typedef unsigned short  Uint16;
21 typedef signed int      Sint32;
22 typedef unsigned int    Uint32;
23 #if __WORDSIZE == 64
24 typedef signed long int Sint64;
25 typedef unsigned long int       Uint64;
26 #else
27 typedef signed long long int    Sint64;
28 typedef unsigned long long int  Uint64;
29 #endif
30
31 typedef Sint64  Sint;
32 typedef Uint64  Uint;
33 typedef Uint64  tPAddr;
34 typedef Uint64  tVAddr;
35
36 typedef Uint64  size_t;
37 typedef char    BOOL;
38
39 #define __ASM__ __asm__ __volatile__
40
41 // === MACROS ===
42 /**
43  * \brief Halt the CPU
44  */
45 #define HALT()  __asm__ __volatile__ ("hlt")
46 /**
47  * \brief Fire a magic breakpoint (bochs)
48  */
49 #define MAGIC_BREAK()   __asm__ __volatile__ ("xchg %bx, %bx")
50
51 // Systemcall Registers
52 // TODO: Fix this structure
53 typedef struct sSyscallRegs
54 {
55         union {
56                 Uint    Num;
57                 Uint    Return;
58         };      // RAX
59         Uint    Arg4;   // RCX
60         Uint    Arg3;   // RDX
61         Uint    Error;  // RBX
62         Uint    Resvd1[2];      // Kernel RSP, RBP
63         Uint    Arg2;   // RSI
64         Uint    Arg1;   // RDI
65         Uint    Arg5;   // R8
66         Uint    Arg6;   // R9
67         Uint    Resvd2[6];      // R10 - R15
68         Uint    Resvd3[5];      // IntNum, ErrCode, RIP, CS, RFLAGS
69         
70         Uint    Resvd4[5];      // Int, Err, rip, CS, ...
71         Uint    StackPointer;   // RSP
72         Uint    Resvd5[1];      // SS   
73 }       tSyscallRegs;
74
75 /**
76  * \brief Short Spinlock structure
77  */
78 struct sShortSpinlock {
79         volatile int    Lock;   //!< Lock value
80          int    IF;     //!< Interrupt state on call to SHORTLOCK
81 };
82 /**
83  * \brief Determine if a short spinlock is locked
84  * \param Lock  Lock pointer
85  */
86 static inline int IS_LOCKED(struct sShortSpinlock *Lock) {
87         return !!Lock->Lock;
88 }
89 /**
90  * \brief Acquire a Short Spinlock
91  * \param Lock  Lock pointer
92  * 
93  * This type of mutex should only be used for very short sections of code,
94  * or in places where a Mutex_* would be overkill, such as appending
95  * an element to linked list (usually two assignement lines in C)
96  * 
97  * \note This type of lock halts interrupts, so ensure that no timing
98  * functions are called while it is held.
99  */
100 static inline void SHORTLOCK(struct sShortSpinlock *Lock) {
101          int    v = 1;
102         
103         // Save interrupt state
104         __ASM__ ("pushf;\n\tpop %%rax" : "=a"(Lock->IF));
105         Lock->IF &= 0x200;
106         
107         // Stop interrupts
108         __ASM__ ("cli");
109         
110         // Wait for another CPU to release
111         while(v)
112                 __ASM__("xchgl %%eax, (%%rdi)":"=a"(v):"a"(1),"D"(&Lock->Lock));
113 }
114 /**
115  * \brief Release a short lock
116  * \param Lock  Lock pointer
117  */
118 static inline void SHORTREL(struct sShortSpinlock *Lock) {
119         Lock->Lock = 0;
120         #if 0   // Which is faster?, meh the test is simpler
121         __ASM__ ("pushf;\n\tor %0, (%%rsp);\n\tpopf" : : "a"(Lock->IF));
122         #else
123         if(Lock->IF)    __ASM__ ("sti");
124         #endif
125 }
126
127 #endif
128

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