blob: 3dbf545c83d50f9d140e8a77b56a6b109bcb6a94 [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.hi, __r64_ptr->hi); \
30 READ32_SEG(prefix, SEG, __value.lo, __r64_ptr->lo); \
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->hi, __value.hi); \
48 WRITE32_SEG(prefix, SEG, __w64_ptr->lo, __value.lo); \
49 } 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'Connorefde6092008-03-12 20:33:15 -040082// Low level macros for getting/setting a segment register.
83#define __SET_SEG(SEG, value) \
Kevin O'Connor67c2cf32008-03-30 23:32:19 -040084 __asm__("movw %w1, %%" #SEG : "=m"(__segment_ ## SEG) \
Kevin O'Connor79f41182009-01-19 17:58:54 -050085 : "rm"(value))
Kevin O'Connorefde6092008-03-12 20:33:15 -040086#define __GET_SEG(SEG) ({ \
87 u16 __seg; \
Kevin O'Connor79f41182009-01-19 17:58:54 -050088 __asm__("movw %%" #SEG ", %w0" : "=rm"(__seg) \
Kevin O'Connor67c2cf32008-03-30 23:32:19 -040089 : "m"(__segment_ ## SEG)); \
Kevin O'Connorefde6092008-03-12 20:33:15 -040090 __seg;})
91
Kevin O'Connorefde6092008-03-12 20:33:15 -040092// Macros for accessing a variable in another segment. (They
93// automatically update the %es segment and then make the appropriate
94// access.)
95#define __GET_FARVAR(seg, var) ({ \
Kevin O'Connor4b60c002008-02-25 22:29:55 -050096 SET_SEG(ES, (seg)); \
97 GET_VAR(ES, (var)); })
Kevin O'Connorefde6092008-03-12 20:33:15 -040098#define __SET_FARVAR(seg, var, val) do { \
Kevin O'Connor16375ab2008-03-09 11:53:22 -040099 typeof(var) __sfv_val = (val); \
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500100 SET_SEG(ES, (seg)); \
Kevin O'Connor16375ab2008-03-09 11:53:22 -0400101 SET_VAR(ES, (var), __sfv_val); \
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500102 } while (0)
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500103
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500104// Macros for accesssing a 32bit flat mode pointer from 16bit real
105// mode. (They automatically update the %es segment, break the
106// pointer into segment/offset, and then make the access.)
107#define __GET_FLATPTR(ptr) ({ \
108 typeof(&(ptr)) __ptr = &(ptr); \
109 GET_FARVAR(FLATPTR_TO_SEG(__ptr) \
110 , *(typeof(__ptr))FLATPTR_TO_OFFSET(__ptr)); })
111#define __SET_FLATPTR(ptr, val) do { \
112 typeof (&(ptr)) __ptr = &(ptr); \
113 SET_FARVAR(FLATPTR_TO_SEG(__ptr) \
114 , *(typeof(__ptr))FLATPTR_TO_OFFSET(__ptr) \
115 , (val)); \
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500116 } while (0)
117
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500118// Macros for converting to/from 32bit flat mode pointers to their
Kevin O'Connorefde6092008-03-12 20:33:15 -0400119// equivalent 16bit segment/offset values.
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500120#define FLATPTR_TO_SEG(p) (((u32)(p)) >> 4)
121#define FLATPTR_TO_OFFSET(p) (((u32)(p)) & 0xf)
122#define MAKE_FLATPTR(seg,off) ((void*)(((u32)(seg)<<4)+(u32)(off)))
Kevin O'Connorefde6092008-03-12 20:33:15 -0400123
124
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500125#if MODESEGMENT == 1
Kevin O'Connorefde6092008-03-12 20:33:15 -0400126
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500127// Definitions when using segmented mode.
Kevin O'Connorefde6092008-03-12 20:33:15 -0400128#define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))
129#define SET_FARVAR(seg, var, val) __SET_FARVAR((seg), (var), (val))
Kevin O'Connoreda2c832009-12-27 00:14:26 -0500130#define GET_VAR(seg, var) __GET_VAR("", seg, (var))
131#define SET_VAR(seg, var, val) __SET_VAR("", seg, (var), (val))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500132#define SET_SEG(SEG, value) __SET_SEG(SEG, (value))
133#define GET_SEG(SEG) __GET_SEG(SEG)
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500134#define GET_FLATPTR(ptr) __GET_FLATPTR(ptr)
135#define SET_FLATPTR(ptr, val) __SET_FLATPTR((ptr), (val))
Kevin O'Connorefde6092008-03-12 20:33:15 -0400136
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500137static inline void insb_fl(u16 port, void *ptr_fl, u16 count) {
138 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
139 insb(port, (u8*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400140}
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500141static inline void insw_fl(u16 port, void *ptr_fl, u16 count) {
142 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
143 insw(port, (u16*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400144}
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500145static inline void insl_fl(u16 port, void *ptr_fl, u16 count) {
146 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
147 insl(port, (u32*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400148}
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500149static inline void outsb_fl(u16 port, void *ptr_fl, u16 count) {
150 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
151 outsb(port, (u8*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400152}
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500153static inline void outsw_fl(u16 port, void *ptr_fl, u16 count) {
154 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
155 outsw(port, (u16*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400156}
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500157static inline void outsl_fl(u16 port, void *ptr_fl, u16 count) {
158 SET_SEG(ES, FLATPTR_TO_SEG(ptr_fl));
159 outsl(port, (u32*)FLATPTR_TO_OFFSET(ptr_fl), count);
Kevin O'Connorefde6092008-03-12 20:33:15 -0400160}
161
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500162#else
Kevin O'Connorefde6092008-03-12 20:33:15 -0400163
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500164// In 32-bit flat mode there is no need to mess with the segments.
Kevin O'Connorefde6092008-03-12 20:33:15 -0400165#define GET_FARVAR(seg, var) \
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500166 (*((typeof(&(var)))MAKE_FLATPTR((seg), &(var))))
Kevin O'Connorefde6092008-03-12 20:33:15 -0400167#define SET_FARVAR(seg, var, val) \
168 do { GET_FARVAR((seg), (var)) = (val); } while (0)
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500169#define GET_VAR(seg, var) (var)
170#define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
171#define SET_SEG(SEG, value) ((void)(value))
172#define GET_SEG(SEG) 0
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500173#define GET_FLATPTR(ptr) (ptr)
174#define SET_FLATPTR(ptr, val) do { (ptr) = (val); } while (0)
Kevin O'Connor786502d2008-02-27 10:41:41 -0500175
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500176#define insb_fl(port, ptr_fl, count) insb(port, ptr_fl, count)
177#define insw_fl(port, ptr_fl, count) insw(port, ptr_fl, count)
178#define insl_fl(port, ptr_fl, count) insl(port, ptr_fl, count)
179#define outsb_fl(port, ptr_fl, count) outsb(port, ptr_fl, count)
180#define outsw_fl(port, ptr_fl, count) outsw(port, ptr_fl, count)
181#define outsl_fl(port, ptr_fl, count) outsl(port, ptr_fl, count)
Kevin O'Connorefde6092008-03-12 20:33:15 -0400182
183#endif
Kevin O'Connor843a62c2008-03-09 00:59:58 -0500184
Kevin O'Connor9f985422009-09-09 11:34:39 -0400185// Definition for common 16bit segment/offset pointers.
186struct segoff_s {
187 union {
188 struct {
189 u16 offset;
190 u16 seg;
191 };
192 u32 segoff;
193 };
194};
195#define SEGOFF(s,o) ({struct segoff_s __so; __so.offset=(o); __so.seg=(s); __so;})
196
197static inline struct segoff_s FLATPTR_TO_SEGOFF(void *p) {
198 return SEGOFF(FLATPTR_TO_SEG(p), FLATPTR_TO_OFFSET(p));
199}
200static inline void *SEGOFF_TO_FLATPTR(struct segoff_s so) {
201 return MAKE_FLATPTR(so.seg, so.offset);
202}
203
Kevin O'Connor786502d2008-02-27 10:41:41 -0500204#endif // farptr.h