Change license from GPLv3 to LGPLv3.
[seabios.git] / src / farptr.h
index 56219d290134678c1e955aeab87e4842c6921add..a32bf730faca7a919203cd56ea5694d7d4e75f0e 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
 //
-// This file may be distributed under the terms of the GNU GPLv3 license.
+// This file may be distributed under the terms of the GNU LGPLv3 license.
 #ifndef __FARPTR_H
 #define __FARPTR_H
 
 // Dummy definitions used to make sure gcc understands dependencies
 // between SET_SEG and GET/READ/WRITE_SEG macros.
 extern u16 __segment_ES, __segment_CS, __segment_DS, __segment_SS;
+extern u16 __segment_FS, __segment_GS;
 
 // Low level macros for reading/writing memory via a segment selector.
-#define READ8_SEG(SEG, var) ({                          \
-    typeof(var) __value;                                \
-    __asm__("movb %%" #SEG ":%1, %b0" : "=Qi"(__value)  \
-            : "m"(var), "m"(__segment_ ## SEG));        \
-    __value; })
-#define READ16_SEG(SEG, var) ({                         \
-    typeof(var) __value;                                \
-    __asm__("movw %%" #SEG ":%1, %w0" : "=ri"(__value)  \
-            : "m"(var), "m"(__segment_ ## SEG));        \
-    __value; })
-#define READ32_SEG(SEG, var) ({                         \
-    typeof(var) __value;                                \
-    __asm__("movl %%" #SEG ":%1, %0" : "=ri"(__value)   \
-            : "m"(var), "m"(__segment_ ## SEG));        \
-    __value; })
+#define READ8_SEG(SEG, value, var)                      \
+    __asm__("movb %%" #SEG ":%1, %b0" : "=Qi"(value)    \
+            : "m"(var), "m"(__segment_ ## SEG))
+#define READ16_SEG(SEG, value, var)                     \
+    __asm__("movw %%" #SEG ":%1, %w0" : "=ri"(value)    \
+            : "m"(var), "m"(__segment_ ## SEG))
+#define READ32_SEG(SEG, value, var)                     \
+    __asm__("movl %%" #SEG ":%1, %0" : "=ri"(value)     \
+            : "m"(var), "m"(__segment_ ## SEG))
+#define READ64_SEG(SEG, value, var) do {                                \
+        union u64_u32_u *__w64_ptr = (union u64_u32_u *)&(value);       \
+        union u64_u32_u *__r64_ptr = (union u64_u32_u *)&(var);         \
+        READ32_SEG(SEG, __w64_ptr->hi, __r64_ptr->hi);                  \
+        READ32_SEG(SEG, __w64_ptr->lo, __r64_ptr->lo);                  \
+    } while (0)
 #define WRITE8_SEG(SEG, var, value)                             \
     __asm__("movb %b1, %%" #SEG ":%0" : "=m"(var)               \
             : "Q"(value), "m"(__segment_ ## SEG))
@@ -37,6 +38,13 @@ extern u16 __segment_ES, __segment_CS, __segment_DS, __segment_SS;
 #define WRITE32_SEG(SEG, var, value)                            \
     __asm__("movl %1, %%" #SEG ":%0" : "=m"(var)                \
             : "r"(value), "m"(__segment_ ## SEG))
+#define WRITE64_SEG(SEG, var, value) do {                       \
+        union u64_u32_u __value;                                \
+        union u64_u32_u *__w64_ptr = (union u64_u32_u *)&(var); \
+        __value.val = (value);                                  \
+        WRITE32_SEG(SEG, __w64_ptr->hi, __value.hi);            \
+        WRITE32_SEG(SEG, __w64_ptr->lo, __value.lo);            \
+    } while (0)
 
 // Low level macros for getting/setting a segment register.
 #define __SET_SEG(SEG, value)                                   \
@@ -52,33 +60,31 @@ extern u16 __segment_ES, __segment_CS, __segment_DS, __segment_SS;
 // access method.
 extern void __force_link_error__unknown_type();
 
-#define __GET_VAR(seg, var) ({                                          \
-    typeof(var) __val;                                                  \
-    if (__builtin_types_compatible_p(typeof(__val), u8)                 \
-        || __builtin_types_compatible_p(typeof(__val), s8))             \
-        __val = READ8_SEG(seg, var);                                    \
-    else if (__builtin_types_compatible_p(typeof(__val), u16)           \
-             || __builtin_types_compatible_p(typeof(__val), s16))       \
-        __val = READ16_SEG(seg, var);                                   \
-    else if (__builtin_types_compatible_p(typeof(__val), u32)           \
-             || __builtin_types_compatible_p(typeof(__val), s32))       \
-        __val = READ32_SEG(seg, var);                                   \
-    else                                                                \
-        __force_link_error__unknown_type();                             \
+#define __GET_VAR(seg, var) ({                  \
+    typeof(var) __val;                          \
+    if (sizeof(__val) == 1)                     \
+        READ8_SEG(seg, __val, var);             \
+    else if (sizeof(__val) == 2)                \
+        READ16_SEG(seg, __val, var);            \
+    else if (sizeof(__val) == 4)                \
+        READ32_SEG(seg, __val, var);            \
+    else if (sizeof(__val) == 8)                \
+        READ64_SEG(seg, __val, var);            \
+    else                                        \
+        __force_link_error__unknown_type();     \
     __val; })
 
-#define __SET_VAR(seg, var, val) do {                                   \
-        if (__builtin_types_compatible_p(typeof(var), u8)               \
-            || __builtin_types_compatible_p(typeof(var), s8))           \
-            WRITE8_SEG(seg, var, (val));                                \
-        else if (__builtin_types_compatible_p(typeof(var), u16)         \
-                 || __builtin_types_compatible_p(typeof(var), s16))     \
-            WRITE16_SEG(seg, var, (val));                               \
-        else if (__builtin_types_compatible_p(typeof(var), u32)         \
-                 || __builtin_types_compatible_p(typeof(var), s32))     \
-            WRITE32_SEG(seg, var, (val));                               \
-        else                                                            \
-            __force_link_error__unknown_type();                         \
+#define __SET_VAR(seg, var, val) do {           \
+        if (sizeof(var) == 1)                   \
+            WRITE8_SEG(seg, var, (val));        \
+        else if (sizeof(var) == 2)              \
+            WRITE16_SEG(seg, var, (val));       \
+        else if (sizeof(var) == 4)              \
+            WRITE32_SEG(seg, var, (val));       \
+        else if (sizeof(var) == 8)              \
+            WRITE64_SEG(seg, var, (val));       \
+        else                                    \
+            __force_link_error__unknown_type(); \
     } while (0)
 
 // Macros for accessing a variable in another segment.  (They
@@ -93,7 +99,7 @@ extern void __force_link_error__unknown_type();
         SET_VAR(ES, (var), __sfv_val);          \
     } while (0)
 
-// Macros for accesssing a 32bit pointer from 16bit mode.  (They
+// Macros for accesssing a 32bit pointer from 16bit real mode.  (They
 // automatically update the %es segment, break the pointer into
 // segment/offset, and then make the access.)
 #define __GET_FARPTR(ptr) ({                                            \
@@ -111,10 +117,10 @@ extern void __force_link_error__unknown_type();
 // equivalent 16bit segment/offset values.
 #define FARPTR_TO_SEG(p) (((u32)(p)) >> 4)
 #define FARPTR_TO_OFFSET(p) (((u32)(p)) & 0xf)
-#define MAKE_FARPTR(seg,off) ((void*)(((seg)<<4)+(off)))
+#define MAKE_FARPTR(seg,off) ((void*)(((u32)(seg)<<4)+(u32)(off)))
 
 
-#ifdef MODE16
+#if MODE16 == 1
 
 // Definitions when in 16 bit mode.
 #define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))