Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // Basic type definitions for X86 cpus. |
| 2 | // |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 3 | // Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net> |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 4 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 6 | #ifndef __TYPES_H |
| 7 | #define __TYPES_H |
| 8 | |
| 9 | typedef unsigned char u8; |
| 10 | typedef signed char s8; |
| 11 | typedef unsigned short u16; |
| 12 | typedef signed short s16; |
| 13 | typedef unsigned int u32; |
| 14 | typedef signed int s32; |
Kevin O'Connor | e51313d | 2008-03-12 21:19:34 -0400 | [diff] [blame] | 15 | typedef unsigned long long u64; |
Kevin O'Connor | 595fb46 | 2008-04-05 10:47:13 -0400 | [diff] [blame] | 16 | typedef signed long long s64; |
| 17 | typedef u32 size_t; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 18 | |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 19 | union u64_u32_u { |
Kevin O'Connor | 30e6af0 | 2012-09-15 12:17:37 -0400 | [diff] [blame] | 20 | struct { u32 lo, hi; }; |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 21 | u64 val; |
| 22 | }; |
| 23 | |
Kevin O'Connor | b4f4d33 | 2013-09-14 22:03:57 -0400 | [diff] [blame] | 24 | // Definition for common 16bit segment/offset pointers. |
| 25 | struct segoff_s { |
| 26 | union { |
| 27 | struct { |
| 28 | u16 offset; |
| 29 | u16 seg; |
| 30 | }; |
| 31 | u32 segoff; |
| 32 | }; |
| 33 | }; |
| 34 | |
Kevin O'Connor | 942d495 | 2009-06-10 22:44:06 -0400 | [diff] [blame] | 35 | #ifdef MANUAL_NO_JUMP_TABLE |
| 36 | # define default case 775324556: asm(""); default |
| 37 | #endif |
| 38 | |
| 39 | #ifdef WHOLE_PROGRAM |
| 40 | # define __VISIBLE __attribute__((externally_visible)) |
| 41 | #else |
| 42 | # define __VISIBLE |
| 43 | #endif |
Kevin O'Connor | 4a754b3 | 2008-12-28 21:37:27 -0500 | [diff] [blame] | 44 | |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 45 | #define UNIQSEC __FILE__ "." __stringify(__LINE__) |
| 46 | |
Kevin O'Connor | dad41d9 | 2010-01-01 03:04:19 -0500 | [diff] [blame] | 47 | #define __noreturn __attribute__((noreturn)) |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 48 | extern void __force_link_error__only_in_32bit_flat(void) __noreturn; |
| 49 | extern void __force_link_error__only_in_32bit_segmented(void) __noreturn; |
| 50 | extern void __force_link_error__only_in_16bit(void) __noreturn; |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 51 | |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 52 | #define __ASM(code) asm(".section .text.asm." UNIQSEC "\n\t" code) |
| 53 | |
Kevin O'Connor | 7ab798f | 2008-07-13 11:08:36 -0400 | [diff] [blame] | 54 | #if MODE16 == 1 |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 55 | // Notes a function as externally visible in the 16bit code chunk. |
Kevin O'Connor | 4a754b3 | 2008-12-28 21:37:27 -0500 | [diff] [blame] | 56 | # define VISIBLE16 __VISIBLE |
Kevin O'Connor | 52a300f | 2009-12-26 23:32:57 -0500 | [diff] [blame] | 57 | // Notes a function as externally visible in the 32bit flat code chunk. |
Kevin O'Connor | d3e4367 | 2012-05-28 11:37:53 -0400 | [diff] [blame] | 58 | # define VISIBLE32FLAT |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 59 | // Notes a 32bit flat function that will only be called during init. |
Kevin O'Connor | d3e4367 | 2012-05-28 11:37:53 -0400 | [diff] [blame] | 60 | # define VISIBLE32INIT |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 61 | // Notes a function as externally visible in the 32bit segmented code chunk. |
Kevin O'Connor | d3e4367 | 2012-05-28 11:37:53 -0400 | [diff] [blame] | 62 | # define VISIBLE32SEG |
Kevin O'Connor | 92f95b0 | 2008-12-29 20:42:40 -0500 | [diff] [blame] | 63 | // Designate a variable as (only) visible to 16bit code. |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 64 | # define VAR16 __section(".data16." UNIQSEC) |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 65 | // Designate a variable as (only) visible to 32bit segmented code. |
| 66 | # define VAR32SEG __section(".discard.var32seg." UNIQSEC) |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 67 | // Designate a variable as visible and located in the e-segment. |
| 68 | # define VARLOW __section(".discard.varlow." UNIQSEC) __VISIBLE __weak |
Kevin O'Connor | 4195349 | 2013-02-18 23:09:01 -0500 | [diff] [blame] | 69 | // Designate a variable as visible and located in the f-segment. |
| 70 | # define VARFSEG __section(".discard.varfseg." UNIQSEC) __VISIBLE __weak |
Kevin O'Connor | ab482e0 | 2014-06-11 14:00:21 -0400 | [diff] [blame] | 71 | // Designate a variable at a specific address in the f-segment. |
| 72 | # define VARFSEGFIXED(addr) __section(".discard.varfixed." UNIQSEC) __VISIBLE __weak |
Kevin O'Connor | 2af52da | 2013-03-08 19:36:28 -0500 | [diff] [blame] | 73 | // Verify a variable is only accessable via 32bit "init" functions |
| 74 | # define VARVERIFY32INIT __section(".discard.varinit." UNIQSEC) |
Kevin O'Connor | 4a754b3 | 2008-12-28 21:37:27 -0500 | [diff] [blame] | 75 | // Designate top-level assembler as 16bit only. |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 76 | # define ASM16(code) __ASM(code) |
Kevin O'Connor | 52a300f | 2009-12-26 23:32:57 -0500 | [diff] [blame] | 77 | // Designate top-level assembler as 32bit flat only. |
| 78 | # define ASM32FLAT(code) |
| 79 | // Compile time check for a given mode. |
Kevin O'Connor | fdca418 | 2010-01-01 12:46:54 -0500 | [diff] [blame] | 80 | # define ASSERT16() do { } while (0) |
| 81 | # define ASSERT32SEG() __force_link_error__only_in_32bit_segmented() |
| 82 | # define ASSERT32FLAT() __force_link_error__only_in_32bit_flat() |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 83 | #elif MODESEGMENT == 1 |
Kevin O'Connor | d3e4367 | 2012-05-28 11:37:53 -0400 | [diff] [blame] | 84 | # define VISIBLE16 |
| 85 | # define VISIBLE32FLAT |
| 86 | # define VISIBLE32INIT |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 87 | # define VISIBLE32SEG __VISIBLE |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 88 | # define VAR16 __section(".discard.var16." UNIQSEC) |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 89 | # define VAR32SEG __section(".data32seg." UNIQSEC) |
Kevin O'Connor | 46b8262 | 2012-05-13 12:10:30 -0400 | [diff] [blame] | 90 | # define VARLOW __section(".discard.varlow." UNIQSEC) __VISIBLE __weak |
Kevin O'Connor | 4195349 | 2013-02-18 23:09:01 -0500 | [diff] [blame] | 91 | # define VARFSEG __section(".discard.varfseg." UNIQSEC) __VISIBLE __weak |
Kevin O'Connor | ab482e0 | 2014-06-11 14:00:21 -0400 | [diff] [blame] | 92 | # define VARFSEGFIXED(addr) __section(".discard.varfixed." UNIQSEC) __VISIBLE __weak |
Kevin O'Connor | 2af52da | 2013-03-08 19:36:28 -0500 | [diff] [blame] | 93 | # define VARVERIFY32INIT __section(".discard.varinit." UNIQSEC) |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 94 | # define ASM16(code) |
| 95 | # define ASM32FLAT(code) |
Kevin O'Connor | fdca418 | 2010-01-01 12:46:54 -0500 | [diff] [blame] | 96 | # define ASSERT16() __force_link_error__only_in_16bit() |
| 97 | # define ASSERT32SEG() do { } while (0) |
| 98 | # define ASSERT32FLAT() __force_link_error__only_in_32bit_flat() |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 99 | #else |
Kevin O'Connor | d3e4367 | 2012-05-28 11:37:53 -0400 | [diff] [blame] | 100 | # define VISIBLE16 |
Kevin O'Connor | d1b4f96 | 2010-09-15 21:38:16 -0400 | [diff] [blame] | 101 | # define VISIBLE32FLAT __section(".text.runtime." UNIQSEC) __VISIBLE |
| 102 | # define VISIBLE32INIT __section(".text.init." UNIQSEC) __VISIBLE |
Kevin O'Connor | d3e4367 | 2012-05-28 11:37:53 -0400 | [diff] [blame] | 103 | # define VISIBLE32SEG |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 104 | # define VAR16 __section(".discard.var16." UNIQSEC) |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 105 | # define VAR32SEG __section(".discard.var32seg." UNIQSEC) |
Kevin O'Connor | 6afc6f8 | 2013-02-19 01:02:50 -0500 | [diff] [blame] | 106 | # define VARLOW __section(".data.varlow." UNIQSEC) __VISIBLE __weak |
Kevin O'Connor | 4195349 | 2013-02-18 23:09:01 -0500 | [diff] [blame] | 107 | # define VARFSEG __section(".data.varfseg." UNIQSEC) __VISIBLE |
Kevin O'Connor | ab482e0 | 2014-06-11 14:00:21 -0400 | [diff] [blame] | 108 | # define VARFSEGFIXED(addr) __section(".fixedaddr." __stringify(addr)) __VISIBLE __aligned(1) |
Kevin O'Connor | 2af52da | 2013-03-08 19:36:28 -0500 | [diff] [blame] | 109 | # define VARVERIFY32INIT __section(".data.varinit." UNIQSEC) |
Kevin O'Connor | 4a754b3 | 2008-12-28 21:37:27 -0500 | [diff] [blame] | 110 | # define ASM16(code) |
Kevin O'Connor | 52a300f | 2009-12-26 23:32:57 -0500 | [diff] [blame] | 111 | # define ASM32FLAT(code) __ASM(code) |
Kevin O'Connor | fdca418 | 2010-01-01 12:46:54 -0500 | [diff] [blame] | 112 | # define ASSERT16() __force_link_error__only_in_16bit() |
| 113 | # define ASSERT32SEG() __force_link_error__only_in_32bit_segmented() |
| 114 | # define ASSERT32FLAT() do { } while (0) |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 115 | #endif |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 116 | |
| 117 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 118 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
Kevin O'Connor | 28fe1bd | 2008-03-11 22:14:15 -0400 | [diff] [blame] | 119 | #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) |
Kevin O'Connor | 9dd0dd1 | 2008-05-18 00:03:32 -0400 | [diff] [blame] | 120 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
Kevin O'Connor | 6aee52d | 2009-09-27 20:07:40 -0400 | [diff] [blame] | 121 | #define DIV_ROUND_CLOSEST(x, divisor)({ \ |
| 122 | typeof(divisor) __divisor = divisor; \ |
| 123 | (((x) + ((__divisor) / 2)) / (__divisor)); \ |
| 124 | }) |
Kevin O'Connor | 276d4a9 | 2008-06-11 22:47:01 -0400 | [diff] [blame] | 125 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) |
| 126 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) |
Kevin O'Connor | 415d429 | 2009-08-30 19:19:31 -0400 | [diff] [blame] | 127 | #define ALIGN_DOWN(x,a) ((x) & ~((typeof(x))(a)-1)) |
Kevin O'Connor | 0bf9270 | 2009-08-01 11:45:37 -0400 | [diff] [blame] | 128 | #define container_of(ptr, type, member) ({ \ |
| 129 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 130 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
Kevin O'Connor | e097a75 | 2013-06-08 21:49:12 -0400 | [diff] [blame] | 131 | #define container_of_or_null(ptr, type, member) ({ \ |
| 132 | const typeof( ((type *)0)->member ) *___mptr = (ptr); \ |
| 133 | ___mptr ? container_of(___mptr, type, member) : NULL; }) |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 134 | |
Kevin O'Connor | d7eb27e | 2010-03-20 18:17:19 -0400 | [diff] [blame] | 135 | #define likely(x) __builtin_expect(!!(x), 1) |
| 136 | #define unlikely(x) __builtin_expect(!!(x), 0) |
| 137 | |
Kevin O'Connor | 415d429 | 2009-08-30 19:19:31 -0400 | [diff] [blame] | 138 | #define NULL ((void*)0) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 139 | |
Kevin O'Connor | c069394 | 2009-06-10 21:56:01 -0400 | [diff] [blame] | 140 | #define __weak __attribute__((weak)) |
| 141 | #define __section(S) __attribute__((section(S))) |
| 142 | |
Kevin O'Connor | 6eee8ca | 2008-03-03 20:14:12 -0500 | [diff] [blame] | 143 | #define PACKED __attribute__((packed)) |
Kevin O'Connor | 18e38b2 | 2008-12-10 20:40:13 -0500 | [diff] [blame] | 144 | #define __aligned(x) __attribute__((aligned(x))) |
Kevin O'Connor | 6eee8ca | 2008-03-03 20:14:12 -0500 | [diff] [blame] | 145 | |
Kevin O'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 146 | #define barrier() __asm__ __volatile__("": : :"memory") |
| 147 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 148 | #define noinline __attribute__((noinline)) |
Kevin O'Connor | 74f9c08 | 2008-04-13 16:57:16 -0400 | [diff] [blame] | 149 | #define __always_inline inline __attribute__((always_inline)) |
Kevin O'Connor | f9a774c | 2010-04-17 16:58:32 -0400 | [diff] [blame] | 150 | #define __malloc __attribute__((__malloc__)) |
Kevin O'Connor | 871e0a0 | 2009-12-30 12:14:53 -0500 | [diff] [blame] | 151 | #define __attribute_const __attribute__((__const__)) |
Kevin O'Connor | bc28a2b | 2008-03-11 19:32:38 -0400 | [diff] [blame] | 152 | |
Kevin O'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 153 | #define __stringify_1(x) #x |
| 154 | #define __stringify(x) __stringify_1(x) |
| 155 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 156 | #endif // types.h |