Subversion Repositories SvarDOS

Rev

Rev 1847 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
421 mateuszvis 1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
3
 *
1730 mateusz.vi 4
 * Copyright (C) 2021-2024 Mateusz Viste
421 mateuszvis 5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
23
 */
351 mateuszvis 24
 
25
#include <i86.h>
26
#include <string.h>
27
 
367 mateuszvis 28
#include "env.h"
369 mateuszvis 29
#include "helpers.h"
367 mateuszvis 30
 
351 mateuszvis 31
#include "rmodinit.h"
32
 
33
 
449 mateuszvis 34
/* returns far pointer to rmod's settings block on success */
479 mateuszvis 35
struct rmod_props far *rmod_install(unsigned short envsize, unsigned char *rmodcore, unsigned short rmodcore_len) {
351 mateuszvis 36
  char far *myptr, far *mcb;
37
  unsigned short far *owner;
478 mateuszvis 38
  const unsigned short sizeof_rmodandprops_paras = (0x100 + rmodcore_len + sizeof(struct rmod_props) + 15) / 16;
453 mateuszvis 39
  unsigned short rmodseg = 0xffff;
454 mateuszvis 40
  unsigned short envseg, origenvseg;
465 mateuszvis 41
  struct rmod_props far *res;
351 mateuszvis 42
 
453 mateuszvis 43
  /* read my current env segment from PSP and save it */
454 mateuszvis 44
  envseg = *((unsigned short *)0x2c);
45
  origenvseg = envseg;
351 mateuszvis 46
 
369 mateuszvis 47
  /* printf("original (PSP) env buffer at %04X\r\n", envseg); */
449 mateuszvis 48
 
49
  /* if envseg is zero, then enforce our own one (MSDOS 5 does not provide a default env) */
50
  if ((envseg == 0) && (envsize == 0)) envsize = 256;
51
 
351 mateuszvis 52
  /* if custom envsize requested, convert it to number of paragraphs */
53
  if (envsize != 0) {
54
    envsize += 15;
55
    envsize /= 16;
56
  }
57
 
58
  _asm {
1594 mateusz.vi 59
    push bx
60
    push cx
61
    push dx
62
 
351 mateuszvis 63
    /* link in the UMB memory chain for enabling high-memory allocation (and save initial status on stack) */
64
    mov ax, 0x5802  /* GET UMB LINK STATE */
65
    int 0x21
66
    xor ah, ah
67
    push ax         /* save link state on stack */
68
    mov ax, 0x5803  /* SET UMB LINK STATE */
69
    mov bx, 1
70
    int 0x21
71
    /* get current allocation strategy and save it in DX */
72
    mov ax, 0x5800
73
    int 0x21
74
    push ax
75
    pop dx
76
    /* set strategy to 'last fit, try high then low memory' */
77
    mov ax, 0x5801
78
    mov bx, 0x0082
79
    int 0x21
80
    /* ask for a memory block and save the given segment to rmodseg */
81
    mov ah, 0x48
449 mateuszvis 82
    mov bx, sizeof_rmodandprops_paras
351 mateuszvis 83
    int 0x21
84
    jc ALLOC_FAIL
85
    mov rmodseg, ax
86
    /* ask for a memory block for the environment and save it to envseg (only if custom size requested) */
87
    mov bx, envsize
88
    test bx, bx
89
    jz ALLOC_FAIL
90
    mov ah, 0x48
91
    int 0x21
92
    jc ALLOC_FAIL
93
    mov envseg, ax
94
 
95
    ALLOC_FAIL:
96
    /* restore initial allocation strategy */
97
    mov ax, 0x5801
98
    mov bx, dx
99
    int 0x21
100
    /* restore initial UMB memory link state */
101
    mov ax, 0x5803
102
    pop bx       /* pop initial UMB link state from stack */
103
    int 0x21
1594 mateusz.vi 104
 
105
    pop dx
106
    pop cx
107
    pop bx
351 mateuszvis 108
  }
109
 
110
  if (rmodseg == 0xffff) {
369 mateuszvis 111
    outputnl("malloc error");
449 mateuszvis 112
    return(NULL);
351 mateuszvis 113
  }
114
 
1591 mateusz.vi 115
  /* generate a new PSP where RMOD is about to land */
116
  _asm {
117
    push dx
118
    mov ah, 0x26 /* CREATE NEW PROGRAM SEGMENT PREFIX (DOS 1+) */
119
    mov dx, rmodseg
120
    int 0x21
121
    pop dx
459 mateuszvis 122
  }
1587 mateusz.vi 123
 
1591 mateusz.vi 124
  myptr = MK_FP(rmodseg, 0);
125
 
1587 mateusz.vi 126
  /* patch up RMOD's PSP: Parent's PSP segment @ 0x16-0x17 */
127
  myptr[0x16] = rmodseg & 0xff; /* RMOD is his own parent */
128
  myptr[0x17] = rmodseg >> 8;
129
 
1589 mateusz.vi 130
  /* patch up RMOD's PSP: SS:SP pointer @ 0x2E-0x31  --  I abuse the PSP's
1590 mateusz.vi 131
   * command line tail as stack, but I do NOT set the stack at the end of the
1589 mateusz.vi 132
   * tail. E. C. Masloch kindly explained why this would be a bad idea:
133
   *
134
   * "This is wrong and will potentially overwrite part of your buffers that
135
   * start past the PSP. This is because the dword [PSP:2Eh] is not used merely
136
   * to set SS:SP but rather to find the stack frame created by the int 21h
137
   * call. Therefore the int 21h call that terminates the child process will
138
   * then pop a number of registers off starting from the address stored in the
139
   * PSP." <https://github.com/SvarDOS/bugz/issues/38#issuecomment-1817445740>
140
   */
141
  myptr[0x2e] = 192; /* middle of the command line tail area so I have 64 bytes */
142
  myptr[0x2f] = 0;   /* before and 64 bytes in front of me */
1587 mateusz.vi 143
  myptr[0x30] = rmodseg & 0xff;
144
  myptr[0x31] = rmodseg >> 8;
145
 
146
  /* patch up RMOD's PSP: JFT size @ 0x32-0x33 */
1588 mateusz.vi 147
  myptr[0x32] = 20; /* default JFT size (max that fits without an extra allocation) */
1587 mateusz.vi 148
  myptr[0x33] = 0;
149
 
150
  /* patch up RMOD's PSP: JFT pointer @ 0x34-0x37 */
151
  myptr[0x34] = 0x18; /* the JFT is in the PSP itself */
152
  myptr[0x35] = 0;
153
  myptr[0x36] = rmodseg & 0xff;
154
  myptr[0x37] = rmodseg >> 8;
155
 
156
  /* patch up RMOD's PSP: pointer to previous PSP @ 0x38-0x3B */
157
  myptr[0x38] = 0;
158
  myptr[0x39] = 0;
159
  myptr[0x3A] = rmodseg & 0xff;
160
  myptr[0x3B] = rmodseg >> 8;
161
 
162
  /* copy rmod to its destination (right past the PSP I prepared) */
459 mateuszvis 163
  myptr = MK_FP(rmodseg, 0x100);
478 mateuszvis 164
  _fmemcpy(myptr, rmodcore, rmodcore_len);
351 mateuszvis 165
 
1587 mateusz.vi 166
  /* mark rmod memory (MCB) as "self owned" */
351 mateuszvis 167
  mcb = MK_FP(rmodseg - 1, 0);
168
  owner = (void far *)(mcb + 1);
169
  *owner = rmodseg;
170
  _fmemcpy(mcb + 8, "SVARCOM", 8);
171
 
1587 mateusz.vi 172
  /* mark env memory (MCB) as "owned by rmod" */
359 mateuszvis 173
  mcb = MK_FP(envseg - 1, 0);
174
  owner = (void far *)(mcb + 1);
175
  *owner = rmodseg;
176
  _fmemcpy(mcb + 8, "SVARENV", 8);
351 mateuszvis 177
 
449 mateuszvis 178
  /* if env block is newly allocated, fill it with a few NULLs */
179
  if (envsize != 0) {
180
    owner = MK_FP(envseg, 0);
181
    owner[0] = 0;
182
    owner[1] = 0;
183
  }
184
 
1863 mateusz.vi 185
  /* set CTRL+BREAK and CRITERR handlers to rmod */
537 mateuszvis 186
  _asm {
187
    push ax
188
    push dx
189
    push ds
1863 mateusz.vi 190
 
191
    mov ds, rmodseg
192
 
537 mateuszvis 193
    mov ax, 0x2523
194
    mov dx, RMOD_OFFSET_BRKHANDLER
195
    int 0x21
1863 mateusz.vi 196
 
197
    mov ax, 0x2524
198
    mov dx, RMOD_OFFSET_CRITHANDLER
199
    int 0x21
200
 
537 mateuszvis 201
    pop ds
202
    pop dx
203
    pop ax
204
  }
205
 
983 mateusz.vi 206
  /* mark the input buffer as empty */
207
  myptr = MK_FP(rmodseg, RMOD_OFFSET_INPUTBUF);
987 mateusz.vi 208
  myptr[0] = 128;  /* max acceptable length */
209
  myptr[1] = 0;    /* len of currently stored history string */
210
  myptr[2] = '\r'; /* string terminator */
211
  myptr[3] = 0xCA; /* signature to detect stack overflow damaging the buffer */
212
  myptr[4] = 0xFE; /* 2nd byte of the signature */
983 mateusz.vi 213
 
449 mateuszvis 214
  /* prepare result (rmod props) */
478 mateuszvis 215
  res = MK_FP(rmodseg, 0x100 + rmodcore_len);
450 mateuszvis 216
  _fmemset(res, 0, sizeof(*res));  /* zero out */
217
  res->rmodseg = rmodseg;          /* rmod segment */
453 mateuszvis 218
  res->origenvseg = origenvseg;    /* original environment segment */
449 mateuszvis 219
 
459 mateuszvis 220
  /* write env segment to rmod's PSP */
351 mateuszvis 221
  owner = MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
222
  *owner = envseg;
223
 
366 mateuszvis 224
  /* write boot drive to rmod bootdrive field */
225
  _asm {
226
    push ax
227
    push bx
228
    push dx
229
    push ds
230
    mov ax, 0x3305 /* DOS 4.0+ - GET BOOT DRIVE */
231
    int 0x21 /* boot drive is in DL now (1=A:, 2=B:, etc) */
232
    add dl, 'A'-1 /* convert to a proper ASCII letter */
233
    /* set DS to rmodseg */
234
    mov ax, rmodseg
235
    mov ds, ax
236
    /* write boot drive to rmod bootdrive field */
237
    mov bx, RMOD_OFFSET_BOOTDRIVE
238
    mov [bx], dl
239
    pop ds
240
    pop dx
241
    pop bx
242
    pop ax
243
  }
244
 
1597 mateusz.vi 245
  /* save my original int22h handler and parent in rmod's memory */
246
  res->origint22 = *((unsigned long *)0x0a); /* original int22h handler seg:off is at 0x0a of my PSP */
247
  res->origparent = *((unsigned short *)0x16); /* PSP segment of my parent is at 0x16 of my PSP */
450 mateuszvis 248
 
448 mateuszvis 249
  /* set the int22 handler in my PSP to rmod so DOS jumps to rmod after I
250
   * terminate and save the original handler in rmod's memory */
454 mateuszvis 251
  {
252
    unsigned short *ptr = (void *)0x0a; /* int22 handler is at 0x0A of the PSP */
253
    ptr[0] = RMOD_OFFSET_ROUTINE;
254
    ptr[1] = rmodseg;
351 mateuszvis 255
  }
256
 
1595 mateusz.vi 257
  /* set my own parent to RMOD (this is not necessary for MS-DOS nor FreeDOS but
258
   * might be on other DOS implementations) */
259
  {
260
    unsigned short *ptr = (void *)0x16;
261
    *ptr = rmodseg;
262
  }
263
 
449 mateuszvis 264
  return(res);
351 mateuszvis 265
}
266
 
267
 
449 mateuszvis 268
/* look up my parent: if it's rmod then return a ptr to its props struct,
975 mateusz.vi 269
 * otherwise return NULL
983 mateusz.vi 270
 * I look at PSP[Ch] to locate RMOD (ie. the "terminate address") */
479 mateuszvis 271
struct rmod_props far *rmod_find(unsigned short rmodcore_len) {
983 mateusz.vi 272
  unsigned short *parent = (void *)0x0C;
448 mateuszvis 273
  unsigned short far *ptr;
274
  const unsigned short sig[] = {0x1983, 0x1985, 0x2017, 0x2019};
465 mateuszvis 275
  unsigned char *cmdtail = (void *)0x80;
448 mateuszvis 276
  unsigned char i;
277
  /* is it rmod? */
459 mateuszvis 278
  ptr = MK_FP(*parent, 0x100);
449 mateuszvis 279
  for (i = 0; i < 4; i++) if (ptr[i] != sig[i]) return(NULL);
465 mateuszvis 280
  /* match successfull (rmod is my parent) - but is it really a respawn?
281
   * command-line tail should contain a single character '\r' */
282
  if ((cmdtail[0] != 1) || (cmdtail[1] != '\n')) return(NULL);
283
  cmdtail[0] = 0;
284
  cmdtail[1] = '\r';
285
  /* */
478 mateuszvis 286
  return(MK_FP(*parent, 0x100 + rmodcore_len));
351 mateuszvis 287
}
367 mateuszvis 288
 
289
 
290
/* update rmod's pointer to comspec */
291
void rmod_updatecomspecptr(unsigned short rmod_seg, unsigned short env_seg) {
292
  unsigned short far *comspecptr = MK_FP(rmod_seg, RMOD_OFFSET_COMSPECPTR);
439 mateuszvis 293
  char far *comspecfp = env_lookup_val(env_seg, "COMSPEC");
367 mateuszvis 294
  if (comspecfp != NULL) {
1847 mateusz.vi 295
    /* here I need to translate the comspecfp far pointer into an offset
296
     * relative to env_seg */
297
    *comspecptr = FP_OFF(comspecfp) + ((FP_SEG(comspecfp) - env_seg) * 16);
367 mateuszvis 298
  } else {
299
    *comspecptr = 0;
300
  }
301
}
949 mateusz.vi 302
 
303
 
304
/* allocates bytes of far memory, flags it as belonging to rmod
957 mateusz.vi 305
 * the new block can be optionally flagged as 'ident' (if not null) and zero
306
 * out the newly allocated memory.
949 mateusz.vi 307
 * returns a far ptr to the allocated block, or NULL on error */
957 mateusz.vi 308
void far *rmod_fcalloc(unsigned short bytes, unsigned short rmod_seg, char *ident) {
949 mateusz.vi 309
  unsigned short far *owner;
310
  unsigned short newseg = 0;
311
 
312
  /* ask DOS for a memory block (as high as possible) */
313
  _asm {
314
    push bx /* save initial value in BX so I can restore it later */
315
 
316
    /* get current allocation strategy and save it on stack */
317
    mov ax, 0x5800
318
    int 0x21
319
    push ax
320
 
321
    /* set strategy to 'last fit, try high then low memory' */
322
    mov ax, 0x5801
323
    mov bx, 0x0082
324
    int 0x21
325
 
326
    /* ask for a memory block and save the given segment to rmodseg */
327
    mov ah, 0x48  /* Allocate Memory */
328
    mov bx, bytes
329
    add bx, 15    /* convert bytes to paragraphs */
330
    shr bx, 1     /* bx /= 16 */
331
    shr bx, 1
332
    shr bx, 1
333
    shr bx, 1
334
    int 0x21
335
 
336
    /* error handling */
337
    jc FAIL
338
 
339
    /* save newly allocated segment to newseg */
340
    mov newseg, ax
341
 
342
    FAIL:
343
    /* restore initial allocation strategy */
344
    mov ax, 0x5801
345
    pop bx
346
    int 0x21
347
 
348
    pop bx /* restore BX to its initial value */
349
  }
350
 
351
  if (newseg == 0) return(NULL);
352
 
353
  /* mark memory as "owned by rmod" */
354
  owner = (void far *)(MK_FP(newseg - 1, 1));
355
  *owner = rmod_seg;
356
 
357
  /* set the MCB description to ident, if provided */
358
  if (ident) {
359
    char far *mcbdesc = MK_FP(newseg - 1, 8);
360
    int i;
361
    _fmemset(mcbdesc, 0, 8);
362
    for (i = 0; (i < 8) && (ident[i] != 0); i++) { /* field's length is limited to 8 bytes max */
363
      mcbdesc[i] = ident[i];
364
    }
365
  }
366
 
957 mateusz.vi 367
  /* zero out the memory before handing it out */
368
  _fmemset(MK_FP(newseg, 0), 0, bytes);
369
 
949 mateusz.vi 370
  return(MK_FP(newseg, 0));
371
}
372
 
373
 
957 mateusz.vi 374
/* free memory previously allocated by rmod_fcalloc() */
949 mateusz.vi 375
void rmod_ffree(void far *ptr) {
376
  unsigned short ptrseg;
377
  unsigned short myseg = 0;
378
  unsigned short far *owner;
379
  if (ptr == NULL) return;
380
  ptrseg = FP_SEG(ptr);
381
 
382
  /* get my own segment */
383
  _asm {
384
    mov myseg, cs
385
  }
386
 
387
  /* mark memory in MCB as my own, otherwise DOS might refuse to free it */
388
  owner = MK_FP(ptrseg - 1, 1);
389
  *owner = myseg;
390
 
391
  /* free the memory block */
392
  _asm {
393
    push es
394
    mov ah, 0x49  /* Free Memory Block */
395
    mov es, ptrseg
396
    int 0x21
397
    pop es
398
  }
399
}
963 mateusz.vi 400
 
401
 
402
/* free the entire linked list of bat ctx nodes (and set its rmod ptr to NULL) */
403
void rmod_free_bat_llist(struct rmod_props far *rmod) {
404
  while (rmod->bat != NULL) {
405
    struct batctx far *victim = rmod->bat;
406
    rmod->bat = rmod->bat->parent;
407
    rmod_ffree(victim);
408
  }
409
}