blob: 7216f2377745419310b42082f9b2a7af1c84a743 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// Code to access multiple segments within gcc.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU GPLv3 license.
Kevin O'Connor786502d2008-02-27 10:41:41 -05006#ifndef __FARPTR_H
7#define __FARPTR_H
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008
9#define READ8_SEG(SEG, var) ({ \
10 u8 __value; \
11 __asm__ __volatile__("movb %%" #SEG ":%1, %b0" \
12 : "=Qi"(__value) : "m"(var)); \
13 __value; })
14#define READ16_SEG(SEG, var) ({ \
15 u16 __value; \
16 __asm__ __volatile__("movw %%" #SEG ":%1, %w0" \
17 : "=ri"(__value) : "m"(var)); \
18 __value; })
19#define READ32_SEG(SEG, var) ({ \
20 u32 __value; \
21 __asm__ __volatile__("movl %%" #SEG ":%1, %0" \
22 : "=ri"(__value) : "m"(var)); \
23 __value; })
24#define WRITE8_SEG(SEG, var, value) \
25 __asm__ __volatile__("movb %b0, %%" #SEG ":%1" \
26 : : "Q"(value), "m"(var))
27#define WRITE16_SEG(SEG, var, value) \
28 __asm__ __volatile__("movw %w0, %%" #SEG ":%1" \
29 : : "r"(value), "m"(var))
30#define WRITE32_SEG(SEG, var, value) \
31 __asm__ __volatile__("movl %0, %%" #SEG ":%1" \
32 : : "r"(value), "m"(var))
33
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050034extern void __force_link_error__unknown_type();
35
Kevin O'Connor4b60c002008-02-25 22:29:55 -050036#define __GET_VAR(seg, var) ({ \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050037 typeof(var) __val; \
38 if (__builtin_types_compatible_p(typeof(__val), u8)) \
39 __val = READ8_SEG(seg, var); \
40 else if (__builtin_types_compatible_p(typeof(__val), u16)) \
41 __val = READ16_SEG(seg, var); \
42 else if (__builtin_types_compatible_p(typeof(__val), u32)) \
43 __val = READ32_SEG(seg, var); \
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050044 else \
45 __force_link_error__unknown_type(); \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050046 __val; })
47
Kevin O'Connor4b60c002008-02-25 22:29:55 -050048#define __SET_VAR(seg, var, val) do { \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050049 if (__builtin_types_compatible_p(typeof(var), u8)) \
50 WRITE8_SEG(seg, var, (val)); \
51 else if (__builtin_types_compatible_p(typeof(var), u16)) \
52 WRITE16_SEG(seg, var, (val)); \
53 else if (__builtin_types_compatible_p(typeof(var), u32)) \
54 WRITE32_SEG(seg, var, (val)); \
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050055 else \
56 __force_link_error__unknown_type(); \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050057 } while (0)
58
Kevin O'Connor4b60c002008-02-25 22:29:55 -050059#define __SET_SEG(SEG, value) \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050060 __asm__ __volatile__("movw %w0, %%" #SEG : : "r"(value))
Kevin O'Connor4b60c002008-02-25 22:29:55 -050061#define __GET_SEG(SEG) ({ \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050062 u16 __seg; \
63 __asm__ __volatile__("movw %%" #SEG ", %w0" : "=r"(__seg)); \
64 __seg;})
65
Kevin O'Connor4b60c002008-02-25 22:29:55 -050066#define GET_FARVAR(seg, var) ({ \
67 SET_SEG(ES, (seg)); \
68 GET_VAR(ES, (var)); })
69#define SET_FARVAR(seg, var, val) do { \
70 SET_SEG(ES, (seg)); \
71 SET_VAR(ES, (var), val); \
72 } while (0)
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050073
74#define PTR_TO_SEG(p) ((((u32)(p)) >> 4) & 0xf000)
75#define PTR_TO_OFFSET(p) (((u32)(p)) & 0xffff)
76
77#define __GET_FARPTR(ptr) ({ \
78 typeof (&(ptr)) __ptr; \
79 GET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr)); })
80#define __SET_FARVAR(ptr, val) do { \
81 typeof (&(ptr)) __ptr; \
82 SET_FARVAR(PTR_TO_SEG(__ptr), *(typeof __ptr)PTR_TO_OFFSET(__ptr) \
83 , (val)); \
84 } while (0)
85
86#ifdef MODE16
87#define GET_VAR(seg, var) __GET_VAR(seg, (var))
88#define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
89#define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
90#define GET_SEG(SEG) __GET_SEG(SEG)
91#define GET_FARPTR(ptr) __GET_FARPTR(ptr)
92#define SET_FARPTR(ptr, val) __SET_FARPTR((ptr), (val))
93#else
94// In 32-bit mode there is no need to mess with the segments.
95#define GET_VAR(seg, var) (var)
96#define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
97#define SET_SEG(SEG, value) ((void)(value))
98#define GET_SEG(SEG) 0
99#define GET_FARPTR(ptr) (ptr)
100#define SET_FARPTR(ptr, val) do { (var) = (val); } while (0)
101#endif
Kevin O'Connor786502d2008-02-27 10:41:41 -0500102
103#endif // farptr.h