blob: acc9d596e9a2da72b8b42015d3c10d38e8f967f8 [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'Connor4a14d752008-11-08 13:02:46 -050017#define READ8_SEG(SEG, value, var) \
Kevin O'Connor0bf92702009-08-01 11:45:37 -040018 __asm__("addr32 movb %%" #SEG ":%1, %b0" : "=Qi"(value) \
Kevin O'Connor4a14d752008-11-08 13:02:46 -050019 : "m"(var), "m"(__segment_ ## SEG))
20#define READ16_SEG(SEG, value, var) \
Kevin O'Connor0bf92702009-08-01 11:45:37 -040021 __asm__("addr32 movw %%" #SEG ":%1, %w0" : "=ri"(value) \
Kevin O'Connor4a14d752008-11-08 13:02:46 -050022 : "m"(var), "m"(__segment_ ## SEG))
23#define READ32_SEG(SEG, value, var) \
Kevin O'Connor0bf92702009-08-01 11:45:37 -040024 __asm__("addr32 movl %%" #SEG ":%1, %0" : "=ri"(value) \
Kevin O'Connor4a14d752008-11-08 13:02:46 -050025 : "m"(var), "m"(__segment_ ## SEG))
Kevin O'Connor957a1982009-06-30 21:45:56 -040026#define READ64_SEG(SEG, value, var) do { \
27 union u64_u32_u __value; \
28 union u64_u32_u *__r64_ptr = (union u64_u32_u *)&(var); \
29 READ32_SEG(SEG, __value.hi, __r64_ptr->hi); \
30 READ32_SEG(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'Connor67c2cf32008-03-30 23:32:19 -040033#define WRITE8_SEG(SEG, var, value) \
Kevin O'Connor0bf92702009-08-01 11:45:37 -040034 __asm__("addr32 movb %b1, %%" #SEG ":%0" : "=m"(var) \
Kevin O'Connor0257b4a2008-04-12 09:03:34 -040035 : "Q"(value), "m"(__segment_ ## SEG))
Kevin O'Connor67c2cf32008-03-30 23:32:19 -040036#define WRITE16_SEG(SEG, var, value) \
Kevin O'Connor0bf92702009-08-01 11:45:37 -040037 __asm__("addr32 movw %w1, %%" #SEG ":%0" : "=m"(var) \
Kevin O'Connor0257b4a2008-04-12 09:03:34 -040038 : "r"(value), "m"(__segment_ ## SEG))
Kevin O'Connor67c2cf32008-03-30 23:32:19 -040039#define WRITE32_SEG(SEG, var, value) \
Kevin O'Connor0bf92702009-08-01 11:45:37 -040040 __asm__("addr32 movl %1, %%" #SEG ":%0" : "=m"(var) \
Kevin O'Connor0257b4a2008-04-12 09:03:34 -040041 : "r"(value), "m"(__segment_ ## SEG))
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -040042#define WRITE64_SEG(SEG, var, value) do { \
43 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'Connor1bb3b5c2008-05-14 00:43:13 -040047 WRITE32_SEG(SEG, __w64_ptr->hi, __value.hi); \
48 WRITE32_SEG(SEG, __w64_ptr->lo, __value.lo); \
49 } while (0)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050050
Kevin O'Connorefde6092008-03-12 20:33:15 -040051// Low level macros for getting/setting a segment register.
52#define __SET_SEG(SEG, value) \
Kevin O'Connor67c2cf32008-03-30 23:32:19 -040053 __asm__("movw %w1, %%" #SEG : "=m"(__segment_ ## SEG) \
Kevin O'Connor79f41182009-01-19 17:58:54 -050054 : "rm"(value))
Kevin O'Connorefde6092008-03-12 20:33:15 -040055#define __GET_SEG(SEG) ({ \
56 u16 __seg; \
Kevin O'Connor79f41182009-01-19 17:58:54 -050057 __asm__("movw %%" #SEG ", %w0" : "=rm"(__seg) \
Kevin O'Connor67c2cf32008-03-30 23:32:19 -040058 : "m"(__segment_ ## SEG)); \
Kevin O'Connorefde6092008-03-12 20:33:15 -040059 __seg;})
60
61// Macros for automatically choosing the appropriate memory size
62// access method.
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050063extern void __force_link_error__unknown_type();
64
Kevin O'Connor4a14d752008-11-08 13:02:46 -050065#define __GET_VAR(seg, var) ({ \
66 typeof(var) __val; \
67 if (sizeof(__val) == 1) \
68 READ8_SEG(seg, __val, var); \
69 else if (sizeof(__val) == 2) \
70 READ16_SEG(seg, __val, var); \
71 else if (sizeof(__val) == 4) \
72 READ32_SEG(seg, __val, var); \
73 else if (sizeof(__val) == 8) \
74 READ64_SEG(seg, __val, var); \
75 else \
76 __force_link_error__unknown_type(); \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050077 __val; })
78
Kevin O'Connor4a14d752008-11-08 13:02:46 -050079#define __SET_VAR(seg, var, val) do { \
80 if (sizeof(var) == 1) \
81 WRITE8_SEG(seg, var, (val)); \
82 else if (sizeof(var) == 2) \
83 WRITE16_SEG(seg, var, (val)); \
84 else if (sizeof(var) == 4) \
85 WRITE32_SEG(seg, var, (val)); \
86 else if (sizeof(var) == 8) \
87 WRITE64_SEG(seg, var, (val)); \
88 else \
89 __force_link_error__unknown_type(); \
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050090 } while (0)
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'Connor7ab798f2008-07-13 11:08:36 -0400125#if MODE16 == 1
Kevin O'Connorefde6092008-03-12 20:33:15 -0400126
127// Definitions when in 16 bit mode.
128#define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))
129#define SET_FARVAR(seg, var, val) __SET_FARVAR((seg), (var), (val))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500130#define GET_VAR(seg, var) __GET_VAR(seg, (var))
131#define SET_VAR(seg, var, val) __SET_VAR(seg, (var), (val))
132#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'Connor0b60a062009-06-15 23:03:05 -0400162extern void __force_link_error__only_in_32bit() __attribute__ ((noreturn));
163#define ASSERT16() do { } while (0)
164#define ASSERT32() __force_link_error__only_in_32bit()
165
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500166#else
Kevin O'Connorefde6092008-03-12 20:33:15 -0400167
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500168// In 32-bit mode there is no need to mess with the segments.
Kevin O'Connorefde6092008-03-12 20:33:15 -0400169#define GET_FARVAR(seg, var) \
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500170 (*((typeof(&(var)))MAKE_FLATPTR((seg), &(var))))
Kevin O'Connorefde6092008-03-12 20:33:15 -0400171#define SET_FARVAR(seg, var, val) \
172 do { GET_FARVAR((seg), (var)) = (val); } while (0)
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500173#define GET_VAR(seg, var) (var)
174#define SET_VAR(seg, var, val) do { (var) = (val); } while (0)
175#define SET_SEG(SEG, value) ((void)(value))
176#define GET_SEG(SEG) 0
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500177#define GET_FLATPTR(ptr) (ptr)
178#define SET_FLATPTR(ptr, val) do { (ptr) = (val); } while (0)
Kevin O'Connor786502d2008-02-27 10:41:41 -0500179
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500180#define insb_fl(port, ptr_fl, count) insb(port, ptr_fl, count)
181#define insw_fl(port, ptr_fl, count) insw(port, ptr_fl, count)
182#define insl_fl(port, ptr_fl, count) insl(port, ptr_fl, count)
183#define outsb_fl(port, ptr_fl, count) outsb(port, ptr_fl, count)
184#define outsw_fl(port, ptr_fl, count) outsw(port, ptr_fl, count)
185#define outsl_fl(port, ptr_fl, count) outsl(port, ptr_fl, count)
Kevin O'Connorefde6092008-03-12 20:33:15 -0400186
Kevin O'Connor0b60a062009-06-15 23:03:05 -0400187extern void __force_link_error__only_in_16bit() __attribute__ ((noreturn));
188#define ASSERT16() __force_link_error__only_in_16bit()
189#define ASSERT32() do { } while (0)
190
Kevin O'Connorefde6092008-03-12 20:33:15 -0400191#endif
Kevin O'Connor843a62c2008-03-09 00:59:58 -0500192
Kevin O'Connor9f985422009-09-09 11:34:39 -0400193// Definition for common 16bit segment/offset pointers.
194struct segoff_s {
195 union {
196 struct {
197 u16 offset;
198 u16 seg;
199 };
200 u32 segoff;
201 };
202};
203#define SEGOFF(s,o) ({struct segoff_s __so; __so.offset=(o); __so.seg=(s); __so;})
204
205static inline struct segoff_s FLATPTR_TO_SEGOFF(void *p) {
206 return SEGOFF(FLATPTR_TO_SEG(p), FLATPTR_TO_OFFSET(p));
207}
208static inline void *SEGOFF_TO_FLATPTR(struct segoff_s so) {
209 return MAKE_FLATPTR(so.seg, so.offset);
210}
211
Kevin O'Connor786502d2008-02-27 10:41:41 -0500212#endif // farptr.h