1 // Memory access to BIOS variables.
3 // Copyright (C) 2008-2013 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
9 #include "config.h" // SEG_BDA
10 #include "farptr.h" // GET_FARVAR
11 #include "memmap.h" // SYMBOL
12 #include "std/bda.h" // struct bios_data_area_s
15 /****************************************************************
16 * Interrupt vector table
17 ****************************************************************/
19 #define GET_IVT(vector) \
20 GET_FARVAR(SEG_IVT, ((struct rmode_IVT *)0)->ivec[vector])
21 #define SET_IVT(vector, segoff) \
22 SET_FARVAR(SEG_IVT, ((struct rmode_IVT *)0)->ivec[vector], segoff)
24 #define FUNC16(func) ({ \
26 extern void func (void); \
27 SEGOFF(SEG_BIOS, (u32)func - BUILD_BIOS_ADDR); \
31 /****************************************************************
32 * Bios Data Area (BDA)
33 ****************************************************************/
36 #define GET_BDA(var) \
37 GET_FARVAR(SEG_BDA, ((struct bios_data_area_s *)0)->var)
38 #define SET_BDA(var, val) \
39 SET_FARVAR(SEG_BDA, ((struct bios_data_area_s *)0)->var, (val))
41 // Helper function to set the bits of the equipment_list_flags variable.
42 static inline void set_equipment_flags(u16 clear, u16 set) {
43 u16 eqf = GET_BDA(equipment_list_flags);
44 SET_BDA(equipment_list_flags, (eqf & ~clear) | set);
48 /****************************************************************
49 * Extended Bios Data Area (EBDA)
50 ****************************************************************/
52 // The initial size and location of EBDA
53 #define EBDA_SIZE_START \
54 DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024)
55 #define EBDA_SEGMENT_START \
56 FLATPTR_TO_SEG(BUILD_LOWRAM_END - EBDA_SIZE_START*1024)
59 static inline u16 get_ebda_seg(void) {
60 return GET_BDA(ebda_seg);
62 static inline struct extended_bios_data_area_s *
66 return MAKE_FLATPTR(get_ebda_seg(), 0);
68 #define GET_EBDA(eseg, var) \
69 GET_FARVAR(eseg, ((struct extended_bios_data_area_s *)0)->var)
70 #define SET_EBDA(eseg, var, val) \
71 SET_FARVAR(eseg, ((struct extended_bios_data_area_s *)0)->var, (val))
74 /****************************************************************
76 ****************************************************************/
78 #if MODE16 == 0 && MODESEGMENT == 1
79 // In 32bit segmented mode %cs may not be readable and the code may be
80 // relocated. The entry code sets up %gs with a readable segment and
81 // the code offset can be determined by get_global_offset().
82 #define GLOBAL_SEGREG GS
83 static inline u32 __attribute_const get_global_offset(void) {
92 #define GLOBAL_SEGREG CS
93 static inline u32 __attribute_const get_global_offset(void) {
97 static inline u16 get_global_seg(void) {
98 return GET_SEG(GLOBAL_SEGREG);
100 #define GET_GLOBAL(var) \
101 GET_VAR(GLOBAL_SEGREG, *(typeof(&(var)))((void*)&(var) \
102 + get_global_offset()))
104 #define GLOBALFLAT2GLOBAL(var) ((typeof(var))((void*)(var) - BUILD_BIOS_ADDR))
106 #define GLOBALFLAT2GLOBAL(var) (var)
108 // Access a "flat" pointer known to point to the f-segment.
109 #define GET_GLOBALFLAT(var) GET_GLOBAL(*GLOBALFLAT2GLOBAL(&(var)))
112 /****************************************************************
113 * "Low" memory variables
114 ****************************************************************/
116 #define SEG_LOW SYMBOL(_zonelow_seg)
119 #define GET_LOW(var) GET_FARVAR(SEG_LOW, (var))
120 #define SET_LOW(var, val) SET_FARVAR(SEG_LOW, (var), (val))
121 #define LOWFLAT2LOW(var) ((typeof(var))((void*)(var) - SYMBOL(zonelow_base)))
123 #define GET_LOW(var) (var)
124 #define SET_LOW(var, val) do { (var) = (val); } while (0)
125 #define LOWFLAT2LOW(var) (var)
127 #define GET_LOWFLAT(var) GET_LOW(*LOWFLAT2LOW(&(var)))
128 #define SET_LOWFLAT(var, val) SET_LOW(*LOWFLAT2LOW(&(var)), (val))
130 #endif // __BIOSVAR_H