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 "std/bda.h" // struct bios_data_area_s
14 /****************************************************************
15 * Interupt vector table
16 ****************************************************************/
18 #define GET_IVT(vector) \
19 GET_FARVAR(SEG_IVT, ((struct rmode_IVT *)0)->ivec[vector])
20 #define SET_IVT(vector, segoff) \
21 SET_FARVAR(SEG_IVT, ((struct rmode_IVT *)0)->ivec[vector], segoff)
23 #define FUNC16(func) ({ \
25 extern void func (void); \
26 SEGOFF(SEG_BIOS, (u32)func - BUILD_BIOS_ADDR); \
30 /****************************************************************
31 * Bios Data Area (BDA)
32 ****************************************************************/
35 #define GET_BDA(var) \
36 GET_FARVAR(SEG_BDA, ((struct bios_data_area_s *)0)->var)
37 #define SET_BDA(var, val) \
38 SET_FARVAR(SEG_BDA, ((struct bios_data_area_s *)0)->var, (val))
40 // Helper function to set the bits of the equipment_list_flags variable.
41 static inline void set_equipment_flags(u16 clear, u16 set) {
42 u16 eqf = GET_BDA(equipment_list_flags);
43 SET_BDA(equipment_list_flags, (eqf & ~clear) | set);
47 /****************************************************************
48 * Extended Bios Data Area (EBDA)
49 ****************************************************************/
51 // The initial size and location of EBDA
52 #define EBDA_SIZE_START \
53 DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024)
54 #define EBDA_SEGMENT_START \
55 FLATPTR_TO_SEG(BUILD_LOWRAM_END - EBDA_SIZE_START*1024)
58 static inline u16 get_ebda_seg(void) {
59 return GET_BDA(ebda_seg);
61 static inline struct extended_bios_data_area_s *
65 return MAKE_FLATPTR(get_ebda_seg(), 0);
67 #define GET_EBDA(eseg, var) \
68 GET_FARVAR(eseg, ((struct extended_bios_data_area_s *)0)->var)
69 #define SET_EBDA(eseg, var, val) \
70 SET_FARVAR(eseg, ((struct extended_bios_data_area_s *)0)->var, (val))
73 /****************************************************************
75 ****************************************************************/
77 #if MODE16 == 0 && MODESEGMENT == 1
78 // In 32bit segmented mode %cs may not be readable and the code may be
79 // relocated. The entry code sets up %gs with a readable segment and
80 // the code offset can be determined by get_global_offset().
81 #define GLOBAL_SEGREG GS
82 static inline u32 __attribute_const get_global_offset(void) {
91 #define GLOBAL_SEGREG CS
92 static inline u32 __attribute_const get_global_offset(void) {
96 static inline u16 get_global_seg(void) {
97 return GET_SEG(GLOBAL_SEGREG);
99 #define GET_GLOBAL(var) \
100 GET_VAR(GLOBAL_SEGREG, *(typeof(&(var)))((void*)&(var) \
101 + get_global_offset()))
103 #define GLOBALFLAT2GLOBAL(var) ((typeof(var))((void*)(var) - BUILD_BIOS_ADDR))
105 #define GLOBALFLAT2GLOBAL(var) (var)
107 // Access a "flat" pointer known to point to the f-segment.
108 #define GET_GLOBALFLAT(var) GET_GLOBAL(*GLOBALFLAT2GLOBAL(&(var)))
111 /****************************************************************
112 * "Low" memory variables
113 ****************************************************************/
115 extern u8 _zonelow_seg, zonelow_base[];
116 #define SEG_LOW ((u32)&_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) - (u32)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