blob: c0c6d26ca24358d3405fb97050ce24c09b2a9f20 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// Basic type definitions for X86 cpus.
2//
Kevin O'Connord1b4f962010-09-15 21:38:16 -04003// Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05006#ifndef __TYPES_H
7#define __TYPES_H
8
9typedef unsigned char u8;
10typedef signed char s8;
11typedef unsigned short u16;
12typedef signed short s16;
13typedef unsigned int u32;
14typedef signed int s32;
Kevin O'Connore51313d2008-03-12 21:19:34 -040015typedef unsigned long long u64;
Kevin O'Connor595fb462008-04-05 10:47:13 -040016typedef signed long long s64;
17typedef u32 size_t;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050018
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -040019union u64_u32_u {
20 struct { u32 hi, lo; };
21 u64 val;
22};
23
Kevin O'Connor942d4952009-06-10 22:44:06 -040024#ifdef MANUAL_NO_JUMP_TABLE
25# define default case 775324556: asm(""); default
26#endif
27
28#ifdef WHOLE_PROGRAM
29# define __VISIBLE __attribute__((externally_visible))
30#else
31# define __VISIBLE
32#endif
Kevin O'Connor4a754b32008-12-28 21:37:27 -050033
Kevin O'Connorc0693942009-06-10 21:56:01 -040034#define UNIQSEC __FILE__ "." __stringify(__LINE__)
35
Kevin O'Connordad41d92010-01-01 03:04:19 -050036#define __noreturn __attribute__((noreturn))
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050037extern void __force_link_error__only_in_32bit_flat(void) __noreturn;
38extern void __force_link_error__only_in_32bit_segmented(void) __noreturn;
39extern void __force_link_error__only_in_16bit(void) __noreturn;
Kevin O'Connor871e0a02009-12-30 12:14:53 -050040
Kevin O'Connorc0693942009-06-10 21:56:01 -040041#define __ASM(code) asm(".section .text.asm." UNIQSEC "\n\t" code)
42
Kevin O'Connor7ab798f2008-07-13 11:08:36 -040043#if MODE16 == 1
Kevin O'Connor19786762008-03-05 21:09:59 -050044// Notes a function as externally visible in the 16bit code chunk.
Kevin O'Connor4a754b32008-12-28 21:37:27 -050045# define VISIBLE16 __VISIBLE
Kevin O'Connor52a300f2009-12-26 23:32:57 -050046// Notes a function as externally visible in the 32bit flat code chunk.
Kevin O'Connorf3fe3aa2010-12-05 12:38:33 -050047# define VISIBLE32FLAT __section(".discard.func32flat." UNIQSEC) noinline
Kevin O'Connord1b4f962010-09-15 21:38:16 -040048// Notes a 32bit flat function that will only be called during init.
49# define VISIBLE32INIT VISIBLE32FLAT
Kevin O'Connor871e0a02009-12-30 12:14:53 -050050// Notes a function as externally visible in the 32bit segmented code chunk.
Kevin O'Connorf3fe3aa2010-12-05 12:38:33 -050051# define VISIBLE32SEG __section(".discard.func32seg." UNIQSEC) noinline
Kevin O'Connor92f95b02008-12-29 20:42:40 -050052// Designate a variable as (only) visible to 16bit code.
Kevin O'Connorc0693942009-06-10 21:56:01 -040053# define VAR16 __section(".data16." UNIQSEC)
Kevin O'Connor372e0712009-09-09 09:51:31 -040054// Designate a variable as visible to 16bit, 32bit, and assembler code.
55# define VAR16VISIBLE VAR16 __VISIBLE
56// Designate a variable as externally visible (in addition to all internal code).
Kevin O'Connorc0693942009-06-10 21:56:01 -040057# define VAR16EXPORT __section(".data16.export." UNIQSEC) __VISIBLE
Kevin O'Connor30853762009-01-17 18:49:20 -050058// Designate a variable at a specific 16bit address
Kevin O'Connorc0693942009-06-10 21:56:01 -040059# define VAR16FIXED(addr) __aligned(1) __VISIBLE __section(".fixedaddr." __stringify(addr))
Kevin O'Connor871e0a02009-12-30 12:14:53 -050060// Designate a variable as (only) visible to 32bit segmented code.
61# define VAR32SEG __section(".discard.var32seg." UNIQSEC)
Kevin O'Connor0bf92702009-08-01 11:45:37 -040062// Designate a 32bit variable also available in 16bit "big real" mode.
Kevin O'Connor52a300f2009-12-26 23:32:57 -050063# define VAR32FLATVISIBLE __section(".discard.var32flat." UNIQSEC) __VISIBLE __weak
Kevin O'Connor4a754b32008-12-28 21:37:27 -050064// Designate top-level assembler as 16bit only.
Kevin O'Connorc0693942009-06-10 21:56:01 -040065# define ASM16(code) __ASM(code)
Kevin O'Connor52a300f2009-12-26 23:32:57 -050066// Designate top-level assembler as 32bit flat only.
67# define ASM32FLAT(code)
68// Compile time check for a given mode.
Kevin O'Connorfdca4182010-01-01 12:46:54 -050069# define ASSERT16() do { } while (0)
70# define ASSERT32SEG() __force_link_error__only_in_32bit_segmented()
71# define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
Kevin O'Connor871e0a02009-12-30 12:14:53 -050072#elif MODESEGMENT == 1
Kevin O'Connorf3fe3aa2010-12-05 12:38:33 -050073# define VISIBLE16 __section(".discard.func16." UNIQSEC) noinline
74# define VISIBLE32FLAT __section(".discard.func32flat." UNIQSEC) noinline
Kevin O'Connord1b4f962010-09-15 21:38:16 -040075# define VISIBLE32INIT VISIBLE32FLAT
Kevin O'Connor871e0a02009-12-30 12:14:53 -050076# define VISIBLE32SEG __VISIBLE
Kevin O'Connorc0693942009-06-10 21:56:01 -040077# define VAR16 __section(".discard.var16." UNIQSEC)
Kevin O'Connor372e0712009-09-09 09:51:31 -040078# define VAR16VISIBLE VAR16 __VISIBLE __weak
79# define VAR16EXPORT VAR16VISIBLE
80# define VAR16FIXED(addr) VAR16VISIBLE
Kevin O'Connor871e0a02009-12-30 12:14:53 -050081# define VAR32SEG __section(".data32seg." UNIQSEC)
82# define VAR32FLATVISIBLE __section(".discard.var32flat." UNIQSEC) __VISIBLE __weak
83# define ASM16(code)
84# define ASM32FLAT(code)
Kevin O'Connorfdca4182010-01-01 12:46:54 -050085# define ASSERT16() __force_link_error__only_in_16bit()
86# define ASSERT32SEG() do { } while (0)
87# define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
Kevin O'Connor871e0a02009-12-30 12:14:53 -050088#else
Kevin O'Connorf3fe3aa2010-12-05 12:38:33 -050089# define VISIBLE16 __section(".discard.func16." UNIQSEC) noinline
Kevin O'Connord1b4f962010-09-15 21:38:16 -040090# define VISIBLE32FLAT __section(".text.runtime." UNIQSEC) __VISIBLE
91# define VISIBLE32INIT __section(".text.init." UNIQSEC) __VISIBLE
Kevin O'Connorf3fe3aa2010-12-05 12:38:33 -050092# define VISIBLE32SEG __section(".discard.func32seg." UNIQSEC) noinline
Kevin O'Connor871e0a02009-12-30 12:14:53 -050093# define VAR16 __section(".discard.var16." UNIQSEC)
94# define VAR16VISIBLE VAR16 __VISIBLE __weak
95# define VAR16EXPORT VAR16VISIBLE
96# define VAR16FIXED(addr) VAR16VISIBLE
97# define VAR32SEG __section(".discard.var32seg." UNIQSEC)
Kevin O'Connord1b4f962010-09-15 21:38:16 -040098# define VAR32FLATVISIBLE __section(".data.runtime." UNIQSEC) __VISIBLE
Kevin O'Connor4a754b32008-12-28 21:37:27 -050099# define ASM16(code)
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500100# define ASM32FLAT(code) __ASM(code)
Kevin O'Connorfdca4182010-01-01 12:46:54 -0500101# define ASSERT16() __force_link_error__only_in_16bit()
102# define ASSERT32SEG() __force_link_error__only_in_32bit_segmented()
103# define ASSERT32FLAT() do { } while (0)
Kevin O'Connor19786762008-03-05 21:09:59 -0500104#endif
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500105
106#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500107#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
Kevin O'Connor28fe1bd2008-03-11 22:14:15 -0400108#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
Kevin O'Connor9dd0dd12008-05-18 00:03:32 -0400109#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
Kevin O'Connor6aee52d2009-09-27 20:07:40 -0400110#define DIV_ROUND_CLOSEST(x, divisor)({ \
111 typeof(divisor) __divisor = divisor; \
112 (((x) + ((__divisor) / 2)) / (__divisor)); \
113 })
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400114#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
115#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
Kevin O'Connor415d4292009-08-30 19:19:31 -0400116#define ALIGN_DOWN(x,a) ((x) & ~((typeof(x))(a)-1))
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400117#define container_of(ptr, type, member) ({ \
118 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
119 (type *)( (char *)__mptr - offsetof(type,member) );})
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500120
Kevin O'Connord7eb27e2010-03-20 18:17:19 -0400121#define likely(x) __builtin_expect(!!(x), 1)
122#define unlikely(x) __builtin_expect(!!(x), 0)
123
Kevin O'Connor415d4292009-08-30 19:19:31 -0400124#define NULL ((void*)0)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500125
Kevin O'Connorc0693942009-06-10 21:56:01 -0400126#define __weak __attribute__((weak))
127#define __section(S) __attribute__((section(S)))
128
Kevin O'Connor6eee8ca2008-03-03 20:14:12 -0500129#define PACKED __attribute__((packed))
Kevin O'Connor18e38b22008-12-10 20:40:13 -0500130#define __aligned(x) __attribute__((aligned(x)))
Kevin O'Connor6eee8ca2008-03-03 20:14:12 -0500131
Kevin O'Connora4d35762008-03-08 15:43:03 -0500132#define barrier() __asm__ __volatile__("": : :"memory")
133
Kevin O'Connoree55c762008-05-13 00:18:20 -0400134#define noinline __attribute__((noinline))
Kevin O'Connor74f9c082008-04-13 16:57:16 -0400135#define __always_inline inline __attribute__((always_inline))
Kevin O'Connorf9a774c2010-04-17 16:58:32 -0400136#define __malloc __attribute__((__malloc__))
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500137#define __attribute_const __attribute__((__const__))
Kevin O'Connorbc28a2b2008-03-11 19:32:38 -0400138
Kevin O'Connora4d35762008-03-08 15:43:03 -0500139#define __stringify_1(x) #x
140#define __stringify(x) __stringify_1(x)
141
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500142#endif // types.h