blob: 5b6c5c10918f424894b170bc39098443cb486f5d [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// Code to access multiple segments within gcc.
2//
Kevin O'Connor0bf92702009-08-01 11:45:37 -04003// Copyright (C) 2008,2009 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'Connor786502d2008-02-27 10:41:41 -05006#ifndef __FARPTR_H
7#define __FARPTR_H
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008
Kevin O'Connor843a62c2008-03-09 00:59:58 -05009#include "ioport.h" // insb
10
Kevin O'Connor67c2cf32008-03-30 23:32:19 -040011// Dummy definitions used to make sure gcc understands dependencies
12// between SET_SEG and GET/READ/WRITE_SEG macros.
13extern u16 __segment_ES, __segment_CS, __segment_DS, __segment_SS;
Kevin O'Connor7f343092009-01-01 18:31:11 -050014extern u16 __segment_FS, __segment_GS;
Kevin O'Connor67c2cf32008-03-30 23:32:19 -040015
Kevin O'Connorefde6092008-03-12 20:33:15 -040016// Low level macros for reading/writing memory via a segment selector.
Kevin O'Connoreda2c832009-12-27 00:14:26 -050017#define READ8_SEG(prefix, SEG, value, var) \
18 __asm__(prefix "movb %%" #SEG ":%1, %b0" : "=Qi"(value) \
Kevin O'Connor4a14d752008-11-08 13:02:46 -050019 : "m"(var), "m"(__segment_ ## SEG))
Kevin O'Connoreda2c832009-12-27 00:14:26 -050020#define READ16_SEG(prefix, SEG, value, var) \
21 __asm__(prefix "movw %%" #SEG ":%1, %w0" : "=ri"(value) \
Kevin O'Connor4a14d752008-11-08 13:02:46 -050022 : "m"(var), "m"(__segment_ ## SEG))
Kevin O'Connoreda2c832009-12-27 00:14:26 -050023#define READ32_SEG(prefix, SEG, value, var) \
24 __asm__(prefix "movl %%" #SEG ":%1, %0" : "=ri"(value) \
Kevin O'Connor4a14d752008-11-08 13:02:46 -050025 : "m"(var), "m"(__segment_ ## SEG))
Kevin O'Connoreda2c832009-12-27 00:14:26 -050026#define READ64_SEG(prefix, SEG, value, var) do { \
Kevin O'Connor957a1982009-06-30 21:45:56 -040027 union u64_u32_u __value; \
28 union u64_u32_u *__r64_ptr = (union u64_u32_u *)&(var); \
Kevin O'Connoreda2c832009-12-27 00:14:26 -050029 READ32_SEG(prefix, SEG, __value.lo, __r64_ptr->lo); \
Kevin O'Connor30e6af02012-09-15 12:17:37 -040030 READ32_SEG(prefix, SEG, __value.hi, __r64_ptr->hi); \
Kevin O'Connor96f6fcd2009-07-12 09:12:32 -040031 *(u64*)&(value) = __value.val; \
Kevin O'Connor4a14d752008-11-08 13:02:46 -050032 } while (0)
Kevin O'Connoreda2c832009-12-27 00:14:26 -050033#define WRITE8_SEG(prefix, SEG, var, value) \
34 __asm__(prefix "movb %b1, %%" #SEG ":%0" : "=m"(var) \
Kevin O'Connor0257b4a2008-04-12 09:03:34 -040035 : "Q"(value), "m"(__segment_ ## SEG))
Kevin O'Connoreda2c832009-12-27 00:14:26 -050036#define WRITE16_SEG(prefix, SEG, var, value) \
37 __asm__(prefix "movw %w1, %%" #SEG ":%0" : "=m"(var) \
Kevin O'Connor0257b4a2008-04-12 09:03:34 -040038 : "r"(value), "m"(__segment_ ## SEG))
Kevin O'Connoreda2c832009-12-27 00:14:26 -050039#define WRITE32_SEG(prefix, SEG, var, value) \
40 __asm__(prefix "movl %1, %%" #SEG ":%0" : "=m"(var) \
Kevin O'Connor0257b4a2008-04-12 09:03:34 -040041 : "r"(value), "m"(__segment_ ## SEG))
Kevin O'Connoreda2c832009-12-27 00:14:26 -050042#define WRITE64_SEG(prefix, SEG, var, value) do { \
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -040043 union u64_u32_u __value; \
44 union u64_u32_u *__w64_ptr = (union u64_u32_u *)&(var); \
Kevin O'Connor0bf92702009-08-01 11:45:37 -040045 typeof(var) __value_tmp = (value); \
46 __value.val = *(u64*)&__value_tmp; \
Kevin O'Connoreda2c832009-12-27 00:14:26 -050047 WRITE32_SEG(prefix, SEG, __w64_ptr->lo, __value.lo); \
Kevin O'Connor30e6af02012-09-15 12:17:37 -040048 WRITE32_SEG(prefix, SEG, __w64_ptr->hi, __value.hi); \
Kevin O'Connoreda2c832009-12-27 00:14:26 -050049 } while (0)
50
51// Macros for automatically choosing the appropriate memory size
52// access method.
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050053extern void __force_link_error__unknown_type(void);
Kevin O'Connoreda2c832009-12-27 00:14:26 -050054
55#define __GET_VAR(prefix, seg, var) ({ \
56 typeof(var) __val; \
57 if (sizeof(__val) == 1) \
58 READ8_SEG(prefix, seg, __val, var); \
59 else if (sizeof(__val) == 2) \
60 READ16_SEG(prefix, seg, __val, var); \
61 else if (sizeof(__val) == 4) \
62 READ32_SEG(prefix, seg, __val, var); \
63 else if (sizeof(__val) == 8) \
64 READ64_SEG(prefix, seg, __val, var); \
65 else \
66 __force_link_error__unknown_type(); \
67 __val; })
68
69#define __SET_VAR(prefix, seg, var, val) do { \
70 if (sizeof(var) == 1) \
71 WRITE8_SEG(prefix, seg, var, (val)); \
72 else if (sizeof(var) == 2) \
73 WRITE16_SEG(prefix, seg, var, (val)); \
74 else if (sizeof(var) == 4) \
75 WRITE32_SEG(prefix, seg, var, (val)); \
76 else if (sizeof(var) == 8) \
77 WRITE64_SEG(prefix, seg, var, (val)); \
78 else \
79 __force_link_error__unknown_type(); \
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -040080 } while (0)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050081
Kevin O'Connorb54ee402012-05-24 22:06:39 -040082#define DECL_SEGFUNCS(SEG) \
83static inline void __set_seg_##SEG(u16 seg) { \
84 __asm__("movw %w1, %%" #SEG : "=m"(__segment_##SEG) \
85 : "rm"(seg)); \
86} \
87static inline u16 __get_seg_##SEG(void) { \
88 u16 res; \
89 __asm__("movw %%" #SEG ", %w0" : "=rm"(res) \
90 : "m"(__segment_##SEG)); \
91 return res; \
92}
93DECL_SEGFUNCS(CS)
94DECL_SEGFUNCS(DS)
95DECL_SEGFUNCS(ES)
96DECL_SEGFUNCS(FS)
97DECL_SEGFUNCS(GS)
98DECL_SEGFUNCS(SS)
99
Kevin O'Connorefde6092008-03-12 20:33:15 -0400100// Low level macros for getting/setting a segment register.
Kevin O'Connorb54ee402012-05-24 22:06:39 -0400101#define __SET_SEG(SEG, value) \
102 __set_seg_##SEG(value)
103#define __GET_SEG(SEG) \
104 __get_seg_##SEG()
Kevin O'Connorefde6092008-03-12 20:33:15 -0400105
Kevin O'Connorefde6092008-03-12 20:33:15 -0400106// Macros for accessing a variable in another segment. (They
107// automatically update the %es segment and then make the appropriate
108// access.)
109#define __GET_FARVAR(seg, var) ({ \
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500110 SET_SEG(ES, (seg)); \
111 GET_VAR(ES, (var)); })
Kevin O'Connorefde6092008-03-12 20:33:15 -0400112#define __SET_FARVAR(seg, var, val) do { \
Kevin O'Connor16375ab2008-03-09 11:53:22 -0400113 typeof(var) __sfv_val = (val); \
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500114 SET_SEG(ES, (seg)); \
Kevin O'Connor16375ab2008-03-09 11:53:22 -0400115 SET_VAR(ES, (var), __sfv_val); \
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500116 } while (0)
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500117
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500118// Macros for accesssing a 32bit flat mode pointer from 16bit real
119// mode. (They automatically update the %es segment, break the
120// pointer into segment/offset, and then make the access.)
121#define __GET_FLATPTR(ptr) ({ \
122 typeof(&(ptr)) __ptr = &(ptr); \
123 GET_FARVAR(FLATPTR_TO_SEG(__ptr) \
124 , *(typeof(__ptr))FLATPTR_TO_OFFSET(__ptr)); })
125#define __SET_FLATPTR(ptr, val) do { \
126 typeof (&(ptr)) __ptr = &(ptr); \
127 SET_FARVAR(FLATPTR_TO_SEG(__ptr) \
128 , *(typeof(__ptr))FLATPTR_TO_OFFSET(__ptr) \
129 , (val)); \
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500130 } while (0)
131
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500132// Macros for converting to/from 32bit flat mode pointers to their
Kevin O'Connorefde6092008-03-12 20:33:15 -0400133// equivalent 16bit segment/offset values.
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500134#define FLATPTR_TO_SEG(p) (((u32)(p)) >> 4)
135#define FLATPTR_TO_OFFSET(p) (((u32)(p)) & 0xf)
136#define MAKE_FLATPTR(seg,off) ((void*)(((u32)(seg)<<4)+(u32)(off)))
Kevin O'Connorefde6092008-03-12 20:33:15 -0400137
138
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500139#if MODESEGMENT == 1
Kevin O'Connorefde6092008-03-12 20:33:15 -0400140
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500141// Definitions when using segmented mode.
Kevin O'Connorefde6092008-03-12 20:33:15 -0400142#define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))
143#define SET_FARVAR(seg, var, val) __SET_FARVAR((seg), (var), (val))
Kevin O'Connoreda2c832009-12-27 00:14:26 -0500144#define GET_VAR(seg, var) __GET_VAR("", seg, (var))
145#define SET_VAR(seg, var, val) __SET_VAR("", seg, (var), (val))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500146#define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
147#define GET_SEG(SEG) __GET_SEG(SEG)
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500148#define GET_FLATPTR(ptr) __GET_FLATPTR(ptr)
149#define SET_FLATPTR(ptr, val) __SET_FLATPTR((ptr), (val))
Kevin O'Connorefde6092008-03-12 20:33:15 -0400150
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500151static inline void insb_fl(u16 port, void *ptr_fl, u16 count) {
152 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
153 insb(port, (u8*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400154}
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500155static inline void insw_fl(u16 port, void *ptr_fl, u16 count) {
156 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
157 insw(port, (u16*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400158}
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500159static inline void insl_fl(u16 port, void *ptr_fl, u16 count) {
160 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
161 insl(port, (u32*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400162}
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500163static inline void outsb_fl(u16 port, void *ptr_fl, u16 count) {
164 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
165 outsb(port, (u8*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400166}
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500167static inline void outsw_fl(u16 port, void *ptr_fl, u16 count) {
168 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
169 outsw(port, (u16*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400170}
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500171static inline void outsl_fl(u16 port, void *ptr_fl, u16 count) {
172 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
173 outsl(port, (u32*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400174}
175
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500176#else
Kevin O'Connorefde6092008-03-12 20:33:15 -0400177
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500178// In 32-bit flat mode there is no need to mess with the segments.
Kevin O'Connorefde6092008-03-12 20:33:15 -0400179#define GET_FARVAR(seg, var) \
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500180 (*((typeof(&(var)))MAKE_FLATPTR((seg), &(var))))
Kevin O'Connorefde6092008-03-12 20:33:15 -0400181#define SET_FARVAR(seg, var, val) \
182 do { GET_FARVAR((seg), (var)) = (val); } while (0)
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500183#define GET_VAR(seg, var) (var)
184#define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
185#define SET_SEG(SEG, value) ((void)(value))
186#define GET_SEG(SEG) 0
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500187#define GET_FLATPTR(ptr) (ptr)
188#define SET_FLATPTR(ptr, val) do { (ptr) = (val); } while (0)
Kevin O'Connor786502d2008-02-27 10:41:41 -0500189
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500190#define insb_fl(port, ptr_fl, count) insb(port, ptr_fl, count)
191#define insw_fl(port, ptr_fl, count) insw(port, ptr_fl, count)
192#define insl_fl(port, ptr_fl, count) insl(port, ptr_fl, count)
193#define outsb_fl(port, ptr_fl, count) outsb(port, ptr_fl, count)
194#define outsw_fl(port, ptr_fl, count) outsw(port, ptr_fl, count)
195#define outsl_fl(port, ptr_fl, count) outsl(port, ptr_fl, count)
Kevin O'Connorefde6092008-03-12 20:33:15 -0400196
197#endif
Kevin O'Connor843a62c2008-03-09 00:59:58 -0500198
Kevin O'Connor9f985422009-09-09 11:34:39 -0400199// Definition for common 16bit segment/offset pointers.
200struct segoff_s {
201 union {
202 struct {
203 u16 offset;
204 u16 seg;
205 };
206 u32 segoff;
207 };
208};
209#define SEGOFF(s,o) ({struct segoff_s __so; __so.offset=(o); __so.seg=(s); __so;})
210
211static inline struct segoff_s FLATPTR_TO_SEGOFF(void *p) {
212 return SEGOFF(FLATPTR_TO_SEG(p), FLATPTR_TO_OFFSET(p));
213}
214static inline void *SEGOFF_TO_FLATPTR(struct segoff_s so) {
215 return MAKE_FLATPTR(so.seg, so.offset);
216}
217
Kevin O'Connor786502d2008-02-27 10:41:41 -0500218#endif // farptr.h