forked from EtchedPixels/FUZIX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank8086.c
484 lines (428 loc) · 13 KB
/
bank8086.c
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* Draft code only
*
* This module manages a classic IBM PC system with up to 640K of RAM
* by splitting the memory into 4K 'pages'. Data and stack are allocated
* by finding a pair of chunks such that the the stack starts at FFFF
* and the data begins at 0000 but the 'hole' in the middle may be
* something else. If need be we re-arrange memory to cope with collisions.
* This allows us to avoid minix and ELKS chmem like messiness.
*
* The kernel is assumed to be loaded in the lowest pages.
*
* Set CONFIG_BANK_8086
* Call pagemap_init passing it the page above the kernel, and the total
* number of pages.
*
* TODO
* - Review, Test, Debug
* - Finish swap support
* - Custom usermem including valaddr to handle holes.
* - Offset the page base to be a 512 byte boundary above the
* end of the code (covering discard). Right now we can lose memory
* because of EBDA (top 1K .. costs us 4K if present) and alignment
* of end of code.
*
* This module relies upon the split I/D execve code as well as the
* platform specific copy_page and zero_page functionality that is
* used to move far blocks around including overlapping ones.
*
* For 286 we'll need to make page_t bigger and allow for marking
* of 'dead' ranges in the mapping (eg 640K to 1MB). The algorithm
* itself is fine with this. With 4K pages a 16MB 286 would have 4096
* pages. It's not clear the hole counting algorithm scales nicely this
* far but then it's not clear Fuzix does either 8) as you'd need at
* least 128 things running to fill the space given we have no plan
* to support all the crazy large model stuff that Xenix 286 could.
*
* If you have an EMM implementation then it must implement the following
* routines
*
* emm_alloc_bank(m)
* - if a 64K EMM bank is not free set m->emm = 0 return ENOMEM
* - otherwise set m->emm to your own non zero token and
* set m->dseg to the matching EMM page frame
*
* emm_free_bamk(m)
* - return a 64K EMM bank m->emm to to the pool
*
* emm_select(emm)
* - select the EMM bank matching the token m->emm
*
* emm_space_used(void)
* - return the number of KB of emm in use
* - on EMM init also add the emm space to ramsize
*/
#include <kernel.h>
#include <kdata.h>
#include <printf.h>
#ifdef CONFIG_BANK_8086
/* private : asm helpers */
extern void do_copy_page(uint16_t, uint16_t);
extern void do_zero_page(uint16_t);
typedef uint8_t page_t;
/* Assuming a 640K PC/XT. This model needs some work to handle a 16MB 286
and it's not clear how to extend it to EMM */
/* 4K keeps the page count down into 8bits and also ensures the shift between
page and segment is trivial */
#define PAGESIZE 4096
#define PAGESHIFT 12
#define PAGES_64K (page_t)(65536L >> PAGESHIFT)
#define NUM_PAGES ((640) >> (PAGESHIFT - 10))
#define PAGES_IN(x) (((x) + PAGESIZE - 1) >> PAGESHIFT)
/* 4K is a bit wasteful perhaps but it does mean that we have < 256 pages
which makes life roll along much more nicely */
static page_t pagemap[NUM_PAGES];
static uint16_t total_pages;
static uint16_t free_pages;
/* Pointed to by udata.u_page2 */
static struct proc_map proc_mmu[PTABSIZE];
static uint16_t page_to_segment(page_t page)
{
return (page << (PAGESHIFT - 4));
}
uint16_t get_data_segment(uint16_t pv)
{
struct proc_map *m = (struct proc_map *)pv;
return page_to_segment(m->dseg);
}
uint16_t get_code_segment(uint16_t pv)
{
struct proc_map *m = (struct proc_map *)pv;
return page_to_segment(m->cseg);
}
/* Mark a region as owned by us: FIXME once debugged #define this */
static void claim_region(page_t base, uint8_t len, uint8_t owner)
{
memset(pagemap + base, owner, len);
if (owner)
free_pages -= len;
else
free_pages += len;
}
/* Claim the regions owned by a process, also used to free them */
static void claim_regions(struct proc_map *m, uint8_t owner)
{
claim_region(m->cbase, m->csize, owner);
claim_region(m->dbase, m->dsize, owner);
claim_region(m->dbase + PAGES_64K - m->ssize, m->ssize, owner);
}
static void claim_data_regions(struct proc_map *m, uint8_t owner)
{
claim_region(m->dbase, m->dsize, owner);
claim_region(m->dbase + PAGES_64K - m->ssize, m->ssize, owner);
}
/* Size a free block by counting zero (free) bytes */
static uint8_t need_zeros(uint8_t *addr, uint8_t n)
{
uint8_t ct = 0;
while(*addr == 0 && ct++ < n);
return ct;
}
static uint8_t *skip_used(uint8_t *p)
{
while(*p && p < &pagemap[NUM_PAGES])
p++;
return p;
}
/* Finds the space but doesn't book it out - so we can look for slack and if
not there try again
Q: would a need zeros or self be useful so we could look for a match
that works if we move our existing allocation rather than just do a new
one ? */
static page_t alloc_data(uint8_t dataneed, uint8_t stackneed)
{
uint8_t *addr = pagemap;
uint8_t x;
uint8_t y;
while(addr = skip_used(addr)) {
x = need_zeros(addr, dataneed);
y = need_zeros(addr + PAGES_64K - stackneed, stackneed);
if (x == dataneed) {
if (y == stackneed)
return addr - pagemap;
else {
addr++;
continue;
}
} else {
addr += x; /* skip the whole space */
}
}
return 0;
}
/* Do the same job for a code allocation - here we don't have to worry about
space for the code and kernel copies (in fact we set CS: pointing offset
into the space in order to keep the udata copy here. This is problematic
if we implement shared code, so will require a cunning plan (tm) */
static page_t alloc_code(uint8_t need)
{
uint8_t *addr = pagemap;
uint8_t x;
while(addr = skip_used(addr)) {
x = need_zeros(addr, need);
if (x == need)
return addr - pagemap;
addr += x;
}
return 0;
}
/* We don't yet deal with code segment sharing.. */
/*
* Helper to find a working memory map for a process. This doesn't
* clear or claim any memory merely set up the proc_mmu structure.
*
* It's important it doesn't do anything non-reversible because we
* want to call this in situations where we save the old mmu status,
* mark the pages as free, try a new allocation and then if that fails
* reclaim the old one.
*/
static int do_process_create(uint8_t csize, uint8_t dsize, uint8_t ssize)
{
struct proc_map *m = (struct proc_map *)udata.u_page2;
#ifdef CONFIG_IBMPC_EMM
if (emm_alloc_bank(m) == 0) {
m->dbase = alloc_data(m->dsize, m->ssize);
if (m->dbase == 0)
return -1;
m->dseg = page_to_segment(m->dbase);
}
#endif
m->cbase = alloc_code(csize);
if (m->cbase == 0) {
#ifdef COFNIG_IBMPC_EMM
emm_free_bank(m);
#endif
return -1;
}
m->csize = csize;
m->dsize = dsize;
m->ssize = ssize;
m->cseg = page_to_segment(m->cbase);
return 0;
}
static void copy_pages(page_t to, page_t from, uint16_t len)
{
uint16_t ts = page_to_segment(to);
uint16_t fs = page_to_segment(from);
while(len--)
do_copy_page(ts++, fs++);
}
static void zero_pages(page_t to, uint16_t len)
{
uint16_t ts = page_to_segment(to);
while(len--)
do_zero_page(ts++);
}
/*
* A brk() or stack grow has collided with some other piece of memory.
* Try and find a place to move our data and stacks so that they can
* grow as desired. If need be start swapping stuff out.
*
* Invariant: never called for emm banked processes
*/
static int relocate_data(uint8_t dsize, uint8_t ssize)
{
struct proc_map *m = (struct proc_map *)udata.u_page2;
page_t dbase;
/* Mark our memory free */
claim_data_regions(m, 0);
/* If need be swap other processes out until we can find a suitable
hole pair. We aren't very bright here but it's not clear that being
smart helps anyway */
while((dbase = alloc_data(dsize, ssize)) == 0) {
if (swapneeded(udata.u_ptab, 1) == NULL)
return ENOMEM;
}
if (dbase == 0)
return ENOMEM;
/* Move the segments. This needs care because we might have put
one of the two over the other */
if (dbase < m->dbase) {
/* Moved down - so we know that the new data is not over the old
stack, but the new stack may be over the old data */
copy_pages(dbase, m->dbase, m->dsize);
copy_pages(dbase + PAGES_64K - m->ssize,
m->dbase + PAGES_64K - m->ssize, m->ssize);
} else {
/* Moved up - so move the stack first */
copy_pages(dbase + PAGES_64K - m->ssize,
m->dbase + PAGES_64K - m->ssize, m->ssize);
copy_pages(dbase, m->dbase, m->dsize);
}
m->dbase = dbase;
m->dsize = dsize;
m->ssize = ssize;
/* Mark the regions we used */
claim_data_regions(m, udata.u_page);
m->dseg = page_to_segment(m->dbase);
}
/* Stack growth via software handled fake fault */
static int stack_fault(uint8_t ssize)
{
struct proc_map *m = (struct proc_map *)udata.u_page2;
uint8_t *p = &pagemap[m->dbase + PAGES_64K - ssize];
int8_t mod = ssize - m->ssize;
/* Collided ? if so we will wrap */
if (m->ssize + m->dsize > m->ssize)
return ENOMEM;
#ifdef CONFIG_IBMPC_EMM
if (m->emm) {
m->ssize = ssize;
return 0;
}
#endif
/* FIXME: check rlimitt */
if (need_zeros(p, mod) != mod)
return relocate_data(m->dsize, ssize);
m->ssize += mod;
claim_region(m->dbase + PAGES_64K - m->ssize, mod, udata.u_page);
zero_pages(m->dbase + PAGES_64K - m->ssize, mod);
return 0;
}
/* Data change via brk() */
static int data_change(uint8_t dsize)
{
struct proc_map *m = (struct proc_map *)udata.u_page2;
uint8_t *p = &pagemap[m->dbase + m->dsize];
int8_t mod = dsize - m->dsize;
if (dsize == m->dsize)
return 0;
#ifdef CONFIG_IBMPC_EMM
if (m->emm) {
if (dsize > m->dsize && dsize + m->ssize < dsize)
return ENOMEM;
m->dsize = dsize;
return 0;
}
#endif
/* Give back pages */
if (dsize < m->dsize) {
claim_region(m->dbase + dsize, -mod, 0);
m->dsize = dsize;
return 0;
}
/* Growing and wraps ? */
if (dsize + m->ssize > m->ssize)
return ENOMEM;
/* Claim pages */
if (need_zeros(p, mod) != mod)
return relocate_data(dsize, m->ssize);
/* The caller with zero it */
claim_region(m->dbase + m->dsize, mod, udata.u_page);
m->dsize = dsize;
return 0;
}
/* Called from execve when we exec a process. Kill off any old map and
produce a new one. If we fail keep the old map and execve can return
into it with an error. As we are a split I/D system with stacks this
call is different to the simple 8bit one */
int pagemap_realloc(usize_t csize, usize_t dsize, usize_t ssize)
{
struct proc_map *m = (struct proc_map *)udata.u_page2;
static struct proc_map tmp;
int err = 0;
csize = PAGES_IN(csize);
dsize = PAGES_IN(dsize);
ssize = PAGES_IN(ssize);
memcpy(&tmp, m, sizeof(tmp));
claim_regions(m, 0); /* Free our regions */
if (do_process_create(csize, dsize, ssize)) {
memcpy(m, &tmp, sizeof(tmp));
err = ENOMEM;
}
claim_regions(m, udata.u_page);
return err;
}
/* Report in Kbytes how much memory is in use */
usize_t pagemap_mem_used(void)
{
usize_t r = (total_pages - free_pages) << (PAGESHIFT - 10);
#ifdef CONFIG_IBMPC_EMM
r += emm_space_used();
#endif
return r;
}
/* ktop is a page number as is ramtop */
void pagemap_init_range(page_t ktop, page_t ramtop)
{
/* FIXME: should round _up_ on ktop! */
ktop >>= (PAGESHIFT - 10);
ramtop >>= (PAGESHIFT - 10);
/* Reserve the kernel */
memset(pagemap, 0xFE, ktop);
memset(pagemap + ktop, 0, ramtop - ktop);
/* Reserve any memory below 640K if we are say a 512K machine */
memset(pagemap + ramtop, 0xFE, ramtop - NUM_PAGES);
total_pages = ramtop - ktop;
free_pages = total_pages;
}
/* Allocate the memory map for process p while ensuring process keep is not
swapped out. In some cases the two are the same but when forking we can't
swapout the new process anyway, but must avoid swapping out the live task */
static int do_pagemap_alloc(ptptr p, ptptr keep)
{
struct proc_map *m = (struct proc_map *)udata.u_page2;
/* This is called during a fork where we need to allocate a new
set of maps. At the point of calling p points to the new process
and udata the current one */
/* Set up our new mmu pointers */
p->p_page = p - ptab;
p->p_page2 = (uint16_t)&proc_mmu[p->p_page];
/* Create the new mmu allocation */
while (do_process_create(m->csize, m->dsize, m->ssize))
if (swapneeded(keep, 1) == NULL)
return ENOMEM;
/* Now claim our mapping */
claim_regions(m, p->p_page);
return 0;
}
/* Claim memory for new process p. Do not swap out the currently running
process in order to get it */
int pagemap_alloc(ptptr p)
{
return do_pagemap_alloc(p, udata.u_ptab);
}
void pagemap_free(ptptr p)
{
struct proc_map *m = (struct proc_map *)p->p_page2;
#ifdef CONFIG_IBMPC_EMM
if (m->emm) {
emm_free_bank(m);
return;
}
#endif
claim_regions(m, 0);
}
void fork_copy(ptptr p)
{
struct proc_map *parent_m = (struct proc_map *)udata.u_page2;
struct proc_map *child_m = (struct proc_map *)p->p_page2;
/* udata is the parent ptptr p is the child */
#ifdef CONFIG_IBMPC_EMM
if (emm) {
/* This one is hard as both might be EMM so we'll need to do
a pair of 32K/32K mappings to copy them over */
emm_copy_data_pages(parent_m, child_m);
}
else
#endif
copy_pages(child->m_dbase, parent->m_dbase, parent->m_dsize);
/* This last one will need to be reworked once we've got shared
code segments */
copy_pages(child->m_cbase, parent->m_cbase, parent->m_csize);
}
#ifdef CONFIG_SWAP
int swapout(ptptr p)
{
}
void swapin(ptptr p)
{
}
#else /* CONFIG_SWAP */
ptptr swapneeded(ptptr p, int f)
{
return NULL;
}
#endif
#endif