Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dpmi.c « go32 « sys « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5215065e120504ca2694e898cc1b7471d2d4e237 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <stdlib.h>
#include "dos.h"
#include "go32.h"
#include <sys/types.h>
#include "dpmi.h"

static union REGS r;
static struct SREGS s;

int _go32_dpmi_allocate_dos_memory(_go32_dpmi_seginfo *info)
{
  r.x.ax = 0x0100;
  r.x.bx = info->size;
  int86(0x31, &r, &r);
  if (r.x.flags & 1)
  {
    info->size = r.x.bx;
    return r.x.ax;
  }
  else
  {
    info->rm_segment = r.x.ax;
    info->pm_selector = r.x.dx;
    return 0;
  }
}

int _go32_dpmi_free_dos_memory(_go32_dpmi_seginfo *info)
{
  r.x.ax = 0x0101;
  r.x.dx = info->pm_selector;
  int86(0x31, &r, &r);
  if (r.x.flags & 1)
  {
    return r.x.ax;
  }
  else
  {
    return 0;
  }
}

int _go32_dpmi_resize_dos_memory(_go32_dpmi_seginfo *info)
{
  r.x.ax = 0x0102;
  r.x.bx = info->size;
  r.x.dx = info->pm_selector;
  int86(0x31, &r, &r);
  if (r.x.flags & 1)
  {
    info->size = r.x.bx;
    return r.x.ax;
  }
  else
  {
    return 0;
  }
}

int _go32_dpmi_get_real_mode_interrupt_vector(int vector, _go32_dpmi_seginfo *info)
{
  r.x.ax = 0x0200;
  r.h.bl = vector;
  int86(0x31, &r, &r);
  info->rm_segment = r.x.cx;
  info->rm_offset = r.x.dx;
  return 0;
}

int _go32_dpmi_set_real_mode_interrupt_vector(int vector, _go32_dpmi_seginfo *info)
{
  r.x.ax = 0x0201;
  r.h.bl = vector;
  r.x.cx = info->rm_segment;
  r.x.dx = info->rm_offset;
  int86(0x31, &r, &r);
  return 0;
}

int _go32_dpmi_get_protected_mode_interrupt_vector(int vector, _go32_dpmi_seginfo *info)
{
  r.x.ax = 0x0204;
  r.h.bl = vector;
  int86(0x31, &r, &r);
  info->pm_selector = r.x.cx;
  info->pm_offset = r.x.dx;
  return 0;
}

int _go32_dpmi_set_protected_mode_interrupt_vector(int vector, _go32_dpmi_seginfo *info)
{
  r.x.ax = 0x0205;
  r.h.bl = vector;
  r.x.cx = info->pm_selector;
  r.x.dx = info->pm_offset;
  int86(0x31, &r, &r);
  if (r.x.flags & 1)
  {
    return r.x.ax;
  }
  else
  {
    return 0;
  }
}

/* In real DPMI, we enter with only CS known, and SS on a locked 4K stack which
   is *NOT* our SS.  We must set up everthing, including a stack swap, then
   restore it the way we found it.   C. Sandmann 4-93 */

static unsigned char wrapper_intcommon[] = {
0x1e,						/* push    ds                  */
0x06,						/* push    es                  */
0x0f, 0xa0,					/* push    fs                  */
0x0f, 0xa8,					/* push    gs                  */
0x60,						/* pusha                       */
0x66, 0xb8, 0x34, 0x12,				/* mov     ax,0x1234           */
0x8e, 0xd8,					/* mov     ds,ax               */
0x8e, 0xc0,					/* mov     es,ax               */
0x8e, 0xe0,					/* mov     fs,ax               */
0x8e, 0xe8,					/* mov     gs,ax               */
0xbb, 0x00, 0x00, 0x00, 0x00,			/* mov     ebx,_local_stack    */
0xfc,						/* cld                         */
0x89, 0xe1,					/* mov     ecx,esp             */
0x8c, 0xd2,					/* mov     dx,ss               */
0x8e, 0xd0,					/* mov     ss,ax               */
0x89, 0xdc,					/* mov     esp,ebx             */
0x52,						/* push    edx                 */
0x51,						/* push    ecx                 */
0xe8, 0x00, 0x00, 0x00, 0x00,			/* call    _rmih               */
0x58,						/* pop     eax                 */
0x5b,						/* pop     ebx                 */
0x8e, 0xd3,					/* mov     ss,bx               */
0x89, 0xc4,					/* mov     esp,eax             */
0x61,						/* popa                        */
0x0f, 0xa9,					/* pop     gs                  */
0x0f, 0xa1,					/* pop     fs                  */
0x07,						/* pop     es                  */
0x1f						/* pop     ds                  */
};

static unsigned char wrapper_intiret[] = {
0xcf						/* iret                        */
};

static unsigned char wrapper_intchain[] = {
0x2e, 0xff, 0x2d, 0x00, 0x00, 0x00, 0x00,	/* jmp     cs:[_old_int+39]    */
0xcf,						/* iret                        */
0x78, 0x56, 0x34, 0x12,
0xcd, 0xab
};

/* _interrupt_stack_size can be changed globally before calling this routine if
   needed.  Don't change it between calls or you will mess up the malloc chain ! */

unsigned _interrupt_stack_size = 32256;

int _go32_dpmi_chain_protected_mode_interrupt_vector(int vector, _go32_dpmi_seginfo *info)
{
  char *mystack;
  unsigned char *wrapper = (unsigned char *)malloc(sizeof(wrapper_intcommon) + sizeof(wrapper_intchain));
  if (wrapper == 0)
    return 0x8015;
  mystack = (char *)malloc(_interrupt_stack_size);
  if (mystack == 0)
    return 0x8015;

  r.x.ax = 0x0204;
  r.h.bl = vector;
  int86(0x31, &r, &r);

  memcpy(wrapper, wrapper_intcommon, sizeof(wrapper_intcommon));
  memcpy(wrapper+sizeof(wrapper_intcommon), wrapper_intchain, sizeof(wrapper_intchain));
  *(short *)(wrapper+9) = _go32_my_ds();
  *(long *)(wrapper+20) = (int)mystack + _interrupt_stack_size;
  *(long *)(wrapper+36) = info->pm_offset - (int)wrapper - 40;
  *(long *)(wrapper+sizeof(wrapper_intcommon)+3) = (long)wrapper+sizeof(wrapper_intcommon)+8;
  *(long *)(wrapper+sizeof(wrapper_intcommon)+8) = r.x.dx;
  *(short *)(wrapper+sizeof(wrapper_intcommon)+12) = r.x.cx;

  r.x.ax = 0x0205;
  r.h.bl = vector;
  r.x.cx = _go32_my_cs();
  r.x.dx = (int)wrapper;
  int86(0x31, &r, &r);
  return 0;
}

int _go32_dpmi_allocate_iret_wrapper(_go32_dpmi_seginfo *info)
{
  char *mystack;
  unsigned char *wrapper = (unsigned char *)malloc(sizeof(wrapper_intcommon) + sizeof(wrapper_intiret));
  if (wrapper == 0)
    return 0x8015;
  mystack = (char *)malloc(_interrupt_stack_size);
  if (mystack == 0)
    return 0x8015;

  memcpy(wrapper, wrapper_intcommon, sizeof(wrapper_intcommon));
  memcpy(wrapper+sizeof(wrapper_intcommon), wrapper_intiret, sizeof(wrapper_intiret));
  *(short *)(wrapper+9) = _go32_my_ds();
  *(long *)(wrapper+20) = (int)mystack + _interrupt_stack_size;
  *(long *)(wrapper+36) = info->pm_offset - (int)wrapper - 40;

  info->pm_offset = (int)wrapper;
  return 0;
}

int _go32_dpmi_free_iret_wrapper(_go32_dpmi_seginfo *info)
{
  char *mystack;
  char *wrapper = (char *)info->pm_offset;
  mystack = (char *)(*(long *)(wrapper+20) - _interrupt_stack_size);
  free(mystack);
  free(wrapper);
  return 0;
}

int _go32_dpmi_simulate_int(int vector, _go32_dpmi_registers *regs)
{
  r.h.bl = vector;
  r.h.bh = 0;
  r.x.cx = 0;
  r.x.di = (int)regs;
  if (vector == 0x21 && regs->x.ax == 0x4b00)
  {
    r.x.ax = 0xff0a;
    int86(0x21, &r, &r);
  }
  else
  {
    r.x.ax = 0x0300;
    int86(0x31, &r, &r);
  }
  if (r.x.flags & 1)
  {
    return r.x.ax;
  }
  else
  {
    return 0;
  }
}

int _go32_dpmi_simulate_fcall(_go32_dpmi_registers *regs)
{
  r.x.ax = 0x0301;
  r.h.bh = 0;
  r.x.cx = 0;
  r.x.di = (int)regs;
  int86(0x31, &r, &r);
  if (r.x.flags & 1)
  {
    return r.x.ax;
  }
  else
  {
    return 0;
  }
}

int _go32_dpmi_simulate_fcall_iret(_go32_dpmi_registers *regs)
{
  r.x.ax = 0x0302;
  r.h.bh = 0;
  r.x.cx = 0;
  r.x.di = (int)regs;
  int86(0x31, &r, &r);
  if (r.x.flags & 1)
  {
    return r.x.ax;
  }
  else
  {
    return 0;
  }
}

/* Bug here; this needs to be fixed like above with SS & CLD */

static unsigned char wrapper_common[] = {
0x66, 0x06,				/* push    es                  */
0x66, 0x1e,				/* push    ds                  */
0x66, 0x06,				/* push    es                  */
0x66, 0x1f,				/* pop     ds                  */
0x56,					/* push    esi                 */
0x57,					/* push    edi                 */
0xe8, 0x00, 0x00, 0x00, 0x00,		/* call    _rmcb               */
0x5f,					/* pop     edi                 */
0x5e,					/* pop     esi                 */
0x66, 0x1f,				/* pop     ds                  */
0x66, 0x07,				/* pop     es                  */
0xfc,					/* cld                         */
0x66, 0x8b, 0x06,			/* mov     ax,[esi]            */
0x66, 0x26, 0x89, 0x47, 0x2a,		/* mov     es:[edi+42],ax      */
0x66, 0x8b, 0x46, 0x02,			/* mov     ax,[esi+2]          */
0x66, 0x26, 0x89, 0x47, 0x2c,		/* mov     es:[edi+44],ax      */
};

static unsigned char wrapper_retf[] = {
0x66, 0x26, 0x83, 0x47, 0x2e, 0x04,	/* add     es:[edi+46],0x4     */
0xcf					/* iret                        */
};

static unsigned char wrapper_iret[] = {
0x66, 0x8b, 0x46, 0x04,			/* mov     ax,[esi+4]          */
0x66, 0x26, 0x89, 0x47, 0x20,		/* mov     es:[edi+32],ax      */
0x66, 0x26, 0x83, 0x47, 0x2e, 0x06,	/* add     es:[edi+46],0x6     */
0xcf					/* iret                        */
};

int _go32_dpmi_allocate_real_mode_callback_retf(_go32_dpmi_seginfo *info, _go32_dpmi_registers *regs)
{
  unsigned char *wrapper = (unsigned char *)malloc(sizeof(wrapper_common) + sizeof(wrapper_retf));
  if (wrapper == 0)
    return 0x8015;

  memcpy(wrapper, wrapper_common, sizeof(wrapper_common));
  memcpy(wrapper+sizeof(wrapper_common), wrapper_retf, sizeof(wrapper_retf));
  *(long *)(wrapper+11) = info->pm_offset - (int)wrapper - 15;
  info->size = (int)wrapper;

  r.x.ax = 0x0303;
  r.x.si = (int)wrapper;
  r.x.di = (int)regs;
  s.ds = _go32_my_cs();
  s.es = _go32_my_ds();
  s.fs = 0;
  s.gs = 0;
  int86x(0x31, &r, &r, &s);
  if (r.x.flags & 1)
  {
    return r.x.ax;
  }
  else
  {
    info->rm_segment = r.x.cx;
    info->rm_offset = r.x.dx;
    return 0;
  }
}

int _go32_dpmi_allocate_real_mode_callback_iret(_go32_dpmi_seginfo *info, _go32_dpmi_registers *regs)
{
  unsigned char *wrapper = (unsigned char *)malloc(sizeof(wrapper_common) + sizeof(wrapper_iret));
  if (wrapper == 0)
    return 0x8015;

  memcpy(wrapper, wrapper_common, sizeof(wrapper_common));
  memcpy(wrapper+sizeof(wrapper_common), wrapper_iret, sizeof(wrapper_iret));
  *(long *)(wrapper+11) = info->pm_offset - (int)wrapper - 15;
  info->size = (int)wrapper;

  r.x.ax = 0x0303;
  r.x.si = (int)wrapper;
  r.x.di = (int)regs;
  s.ds = _go32_my_cs();
  s.es = _go32_my_ds();
  s.fs = 0;
  s.gs = 0;
  int86x(0x31, &r, &r, &s);
  if (r.x.flags & 1)
  {
    return r.x.ax;
  }
  else
  {
    info->rm_segment = r.x.cx;
    info->rm_offset = r.x.dx;
    return 0;
  }
}

int _go32_dpmi_free_real_mode_callback(_go32_dpmi_seginfo *info)
{
  free((char *)info->size);
  r.x.ax = 0x0304;
  r.x.cx = info->rm_segment;
  r.x.dx = info->rm_offset;
  int86(0x31, &r, &r);
  if (r.x.flags & 1)
  {
    return r.x.ax;
  }
  else
  {
    return 0;
  }
}

int _go32_dpmi_get_free_memory_information(_go32_dpmi_meminfo *info)
{
  r.x.ax = 0x0500;
  r.x.di = (int)info;
  int86(0x31, &r, &r);
  return 0;
}

u_long _go32_dpmi_remaining_physical_memory()
{
  _go32_dpmi_meminfo info;
  _go32_dpmi_get_free_memory_information(&info);
  if (info.available_physical_pages)
    return info.available_physical_pages * 4096;
  return info.available_memory;
}

u_long _go32_dpmi_remaining_virtual_memory()
{
  _go32_dpmi_meminfo info;
  _go32_dpmi_get_free_memory_information(&info);
  return info.available_memory;
}