1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#ifndef _COM32_H
36#define _COM32_H
37
38#include <stdint.h>
39#include <stdbool.h>
40#include <stddef.h>
41#include <klibc/compiler.h>
42
43
44
45
46
47
48
49
50
51
52typedef union {
53 uint32_t l;
54 uint16_t w[2];
55 uint8_t b[4];
56} reg32_t;
57
58typedef struct {
59 uint16_t gs;
60 uint16_t fs;
61 uint16_t es;
62 uint16_t ds;
63
64 reg32_t edi;
65 reg32_t esi;
66 reg32_t ebp;
67 reg32_t _unused_esp;
68 reg32_t ebx;
69 reg32_t edx;
70 reg32_t ecx;
71 reg32_t eax;
72
73 reg32_t eflags;
74} com32sys_t;
75
76
77#define EFLAGS_CF 0x00000001
78#define EFLAGS_PF 0x00000004
79#define EFLAGS_AF 0x00000010
80#define EFLAGS_ZF 0x00000040
81#define EFLAGS_SF 0x00000080
82#define EFLAGS_TF 0x00000100
83#define EFLAGS_IF 0x00000200
84#define EFLAGS_DF 0x00000400
85#define EFLAGS_OF 0x00000800
86#define EFLAGS_IOPL 0x00003000
87#define EFLAGS_NT 0x00004000
88#define EFLAGS_RF 0x00010000
89#define EFLAGS_VM 0x00020000
90#define EFLAGS_AC 0x00040000
91#define EFLAGS_VIF 0x00080000
92#define EFLAGS_VIP 0x00100000
93#define EFLAGS_ID 0x00200000
94
95struct com32_pmapi;
96
97extern struct com32_sys_args {
98 uint32_t cs_sysargs;
99 char *cs_cmdline;
100 void __cdecl (*cs_intcall)(uint8_t, const com32sys_t *, com32sys_t *);
101 void *cs_bounce;
102 uint32_t cs_bounce_size;
103 void __cdecl (*cs_farcall)(uint32_t, const com32sys_t *, com32sys_t *);
104 int __cdecl (*cs_cfarcall)(uint32_t, const void *, uint32_t);
105 uint32_t cs_memsize;
106 const char *cs_name;
107 const struct com32_pmapi *cs_pm;
108} __com32;
109
110
111
112
113void __intcall(uint8_t __i, const com32sys_t * __sr, com32sys_t * __dr);
114void __farcall(uint16_t __cs, uint16_t __ip,
115 const com32sys_t * __sr, com32sys_t * __dr);
116int __cfarcall(uint16_t __cs, uint16_t __ip,
117 const void *__stack, uint32_t __stack_size);
118extern const com32sys_t __com32_zero_regs;
119
120
121
122
123void *lmalloc(size_t);
124void *lzalloc(size_t);
125void lfree(void *);
126char *lstrdup(const char *);
127
128
129
130
131
132
133
134
135
136
137
138static inline uint16_t SEG(const volatile void *__p)
139{
140 return (uint16_t) (((uintptr_t) __p) >> 4);
141}
142
143static inline uint16_t OFFS(const volatile void *__p)
144{
145
146 return (uint16_t) (uintptr_t) __p & 0x000F;
147}
148
149static inline uint16_t OFFS_WRT(const volatile void *__p, uint16_t __seg)
150{
151 return (uint16_t) ((uintptr_t) __p - ((uintptr_t) __seg << 4));
152}
153
154#define OFFS_VALID(p,s) _OFFS_VALID((p), sizeof *(p), (s))
155
156static inline bool _OFFS_VALID(const volatile void *__p, size_t __s,
157 uint16_t __seg)
158{
159 uintptr_t __segstart = (uintptr_t)__seg << 4;
160 uintptr_t __offs = (uintptr_t)__p - __segstart;
161
162 return __offs <= 0x10000-__s;
163}
164
165static inline void *MK_PTR(uint16_t __seg, uint16_t __offs)
166{
167 return (void *)((__seg << 4) + __offs);
168}
169
170
171
172struct __far_ptr {
173 union {
174 uint32_t ptr;
175 struct {
176 uint16_t offs, seg;
177 };
178 };
179} __attribute__ ((packed));
180
181typedef struct __far_ptr far_ptr_t;
182
183static inline void *GET_PTR(far_ptr_t __fptr)
184{
185 return MK_PTR(__fptr.seg, __fptr.offs);
186}
187
188static inline far_ptr_t FAR_PTR(void *__ptr)
189{
190 far_ptr_t __fptr;
191
192 __fptr.offs = OFFS(__ptr);
193 __fptr.seg = SEG(__ptr);
194 return __fptr;
195}
196
197#endif
198