coverity appeasement
[minix.git] / kernel / arch / i386 / memory.c
blob542084f5f3554f347326ee8381d051b562648f0f
2 #include "kernel/kernel.h"
3 #include "kernel/vm.h"
5 #include <machine/vm.h>
7 #include <minix/type.h>
8 #include <minix/syslib.h>
9 #include <minix/cpufeature.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <signal.h>
13 #include <stdlib.h>
15 #include <machine/vm.h>
17 #include "oxpcie.h"
18 #include "arch_proto.h"
20 #ifdef USE_APIC
21 #include "apic.h"
22 #ifdef USE_WATCHDOG
23 #include "kernel/watchdog.h"
24 #endif
25 #endif
27 phys_bytes video_mem_vaddr = 0;
29 #define HASPT(procptr) ((procptr)->p_seg.p_cr3 != 0)
30 static int nfreepdes = 0;
31 #define MAXFREEPDES 2
32 static int freepdes[MAXFREEPDES];
34 static u32_t phys_get32(phys_bytes v);
36 void mem_clear_mapcache(void)
38 int i;
39 for(i = 0; i < nfreepdes; i++) {
40 struct proc *ptproc = get_cpulocal_var(ptproc);
41 int pde = freepdes[i];
42 u32_t *ptv;
43 assert(ptproc);
44 ptv = ptproc->p_seg.p_cr3_v;
45 assert(ptv);
46 ptv[pde] = 0;
50 /* This function sets up a mapping from within the kernel's address
51 * space to any other area of memory, either straight physical
52 * memory (pr == NULL) or a process view of memory, in 4MB windows.
53 * I.e., it maps in 4MB chunks of virtual (or physical) address space
54 * to 4MB chunks of kernel virtual address space.
56 * It recognizes pr already being in memory as a special case (no
57 * mapping required).
59 * The target (i.e. in-kernel) mapping area is one of the freepdes[]
60 * VM has earlier already told the kernel about that is available. It is
61 * identified as the 'pde' parameter. This value can be chosen freely
62 * by the caller, as long as it is in range (i.e. 0 or higher and corresonds
63 * to a known freepde slot). It is up to the caller to keep track of which
64 * freepde's are in use, and to determine which ones are free to use.
66 * The logical number supplied by the caller is translated into an actual
67 * pde number to be used, and a pointer to it (linear address) is returned
68 * for actual use by phys_copy or memset.
70 static phys_bytes createpde(
71 const struct proc *pr, /* Requested process, NULL for physical. */
72 const phys_bytes linaddr,/* Address after segment translation. */
73 phys_bytes *bytes, /* Size of chunk, function may truncate it. */
74 int free_pde_idx, /* index of the free slot to use */
75 int *changed /* If mapping is made, this is set to 1. */
78 u32_t pdeval;
79 phys_bytes offset;
80 int pde;
82 assert(free_pde_idx >= 0 && free_pde_idx < nfreepdes);
83 pde = freepdes[free_pde_idx];
84 assert(pde >= 0 && pde < 1024);
86 if(pr && ((pr == get_cpulocal_var(ptproc)) || iskernelp(pr))) {
87 /* Process memory is requested, and
88 * it's a process that is already in current page table, or
89 * the kernel, which is always there.
90 * Therefore linaddr is valid directly, with the requested
91 * size.
93 return linaddr;
96 if(pr) {
97 /* Requested address is in a process that is not currently
98 * accessible directly. Grab the PDE entry of that process'
99 * page table that corresponds to the requested address.
101 assert(pr->p_seg.p_cr3_v);
102 pdeval = pr->p_seg.p_cr3_v[I386_VM_PDE(linaddr)];
103 } else {
104 /* Requested address is physical. Make up the PDE entry. */
105 pdeval = (linaddr & I386_VM_ADDR_MASK_4MB) |
106 I386_VM_BIGPAGE | I386_VM_PRESENT |
107 I386_VM_WRITE | I386_VM_USER;
110 /* Write the pde value that we need into a pde that the kernel
111 * can access, into the currently loaded page table so it becomes
112 * visible.
114 assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v);
115 if(get_cpulocal_var(ptproc)->p_seg.p_cr3_v[pde] != pdeval) {
116 get_cpulocal_var(ptproc)->p_seg.p_cr3_v[pde] = pdeval;
117 *changed = 1;
120 /* Memory is now available, but only the 4MB window of virtual
121 * address space that we have mapped; calculate how much of
122 * the requested range is visible and return that in *bytes,
123 * if that is less than the requested range.
125 offset = linaddr & I386_VM_OFFSET_MASK_4MB; /* Offset in 4MB window. */
126 *bytes = MIN(*bytes, I386_BIG_PAGE_SIZE - offset);
128 /* Return the linear address of the start of the new mapping. */
129 return I386_BIG_PAGE_SIZE*pde + offset;
132 /*===========================================================================*
133 * lin_lin_copy *
134 *===========================================================================*/
135 static int lin_lin_copy(struct proc *srcproc, vir_bytes srclinaddr,
136 struct proc *dstproc, vir_bytes dstlinaddr, vir_bytes bytes)
138 u32_t addr;
139 proc_nr_t procslot;
141 assert(get_cpulocal_var(ptproc));
142 assert(get_cpulocal_var(proc_ptr));
143 assert(read_cr3() == get_cpulocal_var(ptproc)->p_seg.p_cr3);
145 procslot = get_cpulocal_var(ptproc)->p_nr;
147 assert(procslot >= 0 && procslot < I386_VM_DIR_ENTRIES);
149 if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE));
150 if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE));
151 assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE));
152 assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v);
153 if(srcproc) assert(!RTS_ISSET(srcproc, RTS_VMINHIBIT));
154 if(dstproc) assert(!RTS_ISSET(dstproc, RTS_VMINHIBIT));
156 while(bytes > 0) {
157 phys_bytes srcptr, dstptr;
158 vir_bytes chunk = bytes;
159 int changed = 0;
161 #ifdef CONFIG_SMP
162 unsigned cpu = cpuid;
164 if (srcproc && GET_BIT(srcproc->p_stale_tlb, cpu)) {
165 changed = 1;
166 UNSET_BIT(srcproc->p_stale_tlb, cpu);
168 if (dstproc && GET_BIT(dstproc->p_stale_tlb, cpu)) {
169 changed = 1;
170 UNSET_BIT(dstproc->p_stale_tlb, cpu);
172 #endif
174 /* Set up 4MB ranges. */
175 srcptr = createpde(srcproc, srclinaddr, &chunk, 0, &changed);
176 dstptr = createpde(dstproc, dstlinaddr, &chunk, 1, &changed);
177 if(changed)
178 reload_cr3();
180 /* Copy pages. */
181 PHYS_COPY_CATCH(srcptr, dstptr, chunk, addr);
183 if(addr) {
184 /* If addr is nonzero, a page fault was caught. */
186 if(addr >= srcptr && addr < (srcptr + chunk)) {
187 return EFAULT_SRC;
189 if(addr >= dstptr && addr < (dstptr + chunk)) {
190 return EFAULT_DST;
193 panic("lin_lin_copy fault out of range");
195 /* Not reached. */
196 return EFAULT;
199 /* Update counter and addresses for next iteration, if any. */
200 bytes -= chunk;
201 srclinaddr += chunk;
202 dstlinaddr += chunk;
205 if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE));
206 if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE));
207 assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE));
208 assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v);
210 return OK;
214 static u32_t phys_get32(phys_bytes addr)
216 u32_t v;
217 int r;
219 if((r=lin_lin_copy(NULL, addr,
220 proc_addr(SYSTEM), (phys_bytes) &v, sizeof(v))) != OK) {
221 panic("lin_lin_copy for phys_get32 failed: %d", r);
224 return v;
227 #if 0
228 static char *cr0_str(u32_t e)
230 static char str[80];
231 strcpy(str, "");
232 #define FLAG(v) do { if(e & (v)) { strcat(str, #v " "); e &= ~v; } } while(0)
233 FLAG(I386_CR0_PE);
234 FLAG(I386_CR0_MP);
235 FLAG(I386_CR0_EM);
236 FLAG(I386_CR0_TS);
237 FLAG(I386_CR0_ET);
238 FLAG(I386_CR0_PG);
239 FLAG(I386_CR0_WP);
240 if(e) { strcat(str, " (++)"); }
241 return str;
244 static char *cr4_str(u32_t e)
246 static char str[80];
247 strcpy(str, "");
248 FLAG(I386_CR4_VME);
249 FLAG(I386_CR4_PVI);
250 FLAG(I386_CR4_TSD);
251 FLAG(I386_CR4_DE);
252 FLAG(I386_CR4_PSE);
253 FLAG(I386_CR4_PAE);
254 FLAG(I386_CR4_MCE);
255 FLAG(I386_CR4_PGE);
256 if(e) { strcat(str, " (++)"); }
257 return str;
259 #endif
261 /*===========================================================================*
262 * umap_virtual *
263 *===========================================================================*/
264 phys_bytes umap_virtual(rp, seg, vir_addr, bytes)
265 register struct proc *rp; /* pointer to proc table entry for process */
266 int seg; /* T, D, or S segment */
267 vir_bytes vir_addr; /* virtual address in bytes within the seg */
268 vir_bytes bytes; /* # of bytes to be copied */
270 phys_bytes phys = 0;
272 if(vm_lookup(rp, vir_addr, &phys, NULL) != OK) {
273 printf("SYSTEM:umap_virtual: vm_lookup of %s: seg 0x%x: 0x%lx failed\n", rp->p_name, seg, vir_addr);
274 phys = 0;
275 } else {
276 if(phys == 0)
277 panic("vm_lookup returned phys: %d", phys);
280 if(phys == 0) {
281 printf("SYSTEM:umap_virtual: lookup failed\n");
282 return 0;
285 /* Now make sure addresses are contiguous in physical memory
286 * so that the umap makes sense.
288 if(bytes > 0 && vm_lookup_range(rp, vir_addr, NULL, bytes) != bytes) {
289 printf("umap_virtual: %s: %lu at 0x%lx (vir 0x%lx) not contiguous\n",
290 rp->p_name, bytes, vir_addr, vir_addr);
291 return 0;
294 /* phys must be larger than 0 (or the caller will think the call
295 * failed), and address must not cross a page boundary.
297 assert(phys);
299 return phys;
303 /*===========================================================================*
304 * vm_lookup *
305 *===========================================================================*/
306 int vm_lookup(const struct proc *proc, const vir_bytes virtual,
307 phys_bytes *physical, u32_t *ptent)
309 u32_t *root, *pt;
310 int pde, pte;
311 u32_t pde_v, pte_v;
313 assert(proc);
314 assert(physical);
315 assert(!isemptyp(proc));
316 assert(HASPT(proc));
318 /* Retrieve page directory entry. */
319 root = (u32_t *) proc->p_seg.p_cr3;
320 assert(!((u32_t) root % I386_PAGE_SIZE));
321 pde = I386_VM_PDE(virtual);
322 assert(pde >= 0 && pde < I386_VM_DIR_ENTRIES);
323 pde_v = phys_get32((u32_t) (root + pde));
325 if(!(pde_v & I386_VM_PRESENT)) {
326 return EFAULT;
329 /* We don't expect to ever see this. */
330 if(pde_v & I386_VM_BIGPAGE) {
331 *physical = pde_v & I386_VM_ADDR_MASK_4MB;
332 if(ptent) *ptent = pde_v;
333 *physical += virtual & I386_VM_OFFSET_MASK_4MB;
334 } else {
335 /* Retrieve page table entry. */
336 pt = (u32_t *) I386_VM_PFA(pde_v);
337 assert(!((u32_t) pt % I386_PAGE_SIZE));
338 pte = I386_VM_PTE(virtual);
339 assert(pte >= 0 && pte < I386_VM_PT_ENTRIES);
340 pte_v = phys_get32((u32_t) (pt + pte));
341 if(!(pte_v & I386_VM_PRESENT)) {
342 return EFAULT;
345 if(ptent) *ptent = pte_v;
347 /* Actual address now known; retrieve it and add page offset. */
348 *physical = I386_VM_PFA(pte_v);
349 *physical += virtual % I386_PAGE_SIZE;
352 return OK;
355 /*===========================================================================*
356 * vm_lookup_range *
357 *===========================================================================*/
358 size_t vm_lookup_range(const struct proc *proc, vir_bytes vir_addr,
359 phys_bytes *phys_addr, size_t bytes)
361 /* Look up the physical address corresponding to linear virtual address
362 * 'vir_addr' for process 'proc'. Return the size of the range covered
363 * by contiguous physical memory starting from that address; this may
364 * be anywhere between 0 and 'bytes' inclusive. If the return value is
365 * nonzero, and 'phys_addr' is non-NULL, 'phys_addr' will be set to the
366 * base physical address of the range. 'vir_addr' and 'bytes' need not
367 * be page-aligned, but the caller must have verified that the given
368 * linear range is valid for the given process at all.
370 phys_bytes phys, next_phys;
371 size_t len;
373 assert(proc);
374 assert(bytes > 0);
375 assert(HASPT(proc));
377 /* Look up the first page. */
378 if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
379 return 0;
381 if (phys_addr != NULL)
382 *phys_addr = phys;
384 len = I386_PAGE_SIZE - (vir_addr % I386_PAGE_SIZE);
385 vir_addr += len;
386 next_phys = phys + len;
388 /* Look up any next pages and test physical contiguity. */
389 while (len < bytes) {
390 if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
391 break;
393 if (next_phys != phys)
394 break;
396 len += I386_PAGE_SIZE;
397 vir_addr += I386_PAGE_SIZE;
398 next_phys += I386_PAGE_SIZE;
401 /* We might now have overshot the requested length somewhat. */
402 return MIN(bytes, len);
405 /*===========================================================================*
406 * vm_suspend *
407 *===========================================================================*/
408 static void vm_suspend(struct proc *caller, const struct proc *target,
409 const vir_bytes linaddr, const vir_bytes len, const int type)
411 /* This range is not OK for this process. Set parameters
412 * of the request and notify VM about the pending request.
414 assert(!RTS_ISSET(caller, RTS_VMREQUEST));
415 assert(!RTS_ISSET(target, RTS_VMREQUEST));
417 RTS_SET(caller, RTS_VMREQUEST);
419 caller->p_vmrequest.req_type = VMPTYPE_CHECK;
420 caller->p_vmrequest.target = target->p_endpoint;
421 caller->p_vmrequest.params.check.start = linaddr;
422 caller->p_vmrequest.params.check.length = len;
423 caller->p_vmrequest.params.check.writeflag = 1;
424 caller->p_vmrequest.type = type;
426 /* Connect caller on vmrequest wait queue. */
427 if(!(caller->p_vmrequest.nextrequestor = vmrequest))
428 if(OK != send_sig(VM_PROC_NR, SIGKMEM))
429 panic("send_sig failed");
430 vmrequest = caller;
433 /*===========================================================================*
434 * vm_check_range *
435 *===========================================================================*/
436 int vm_check_range(struct proc *caller, struct proc *target,
437 vir_bytes vir_addr, size_t bytes)
439 /* Public interface to vm_suspend(), for use by kernel calls. On behalf
440 * of 'caller', call into VM to check linear virtual address range of
441 * process 'target', starting at 'vir_addr', for 'bytes' bytes. This
442 * function assumes that it will called twice if VM returned an error
443 * the first time (since nothing has changed in that case), and will
444 * then return the error code resulting from the first call. Upon the
445 * first call, a non-success error code is returned as well.
447 int r;
449 if ((caller->p_misc_flags & MF_KCALL_RESUME) &&
450 (r = caller->p_vmrequest.vmresult) != OK)
451 return r;
453 vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL);
455 return VMSUSPEND;
458 /*===========================================================================*
459 * delivermsg *
460 *===========================================================================*/
461 void delivermsg(struct proc *rp)
463 int r = OK;
465 assert(rp->p_misc_flags & MF_DELIVERMSG);
466 assert(rp->p_delivermsg.m_source != NONE);
468 if (copy_msg_to_user(&rp->p_delivermsg,
469 (message *) rp->p_delivermsg_vir)) {
470 printf("WARNING wrong user pointer 0x%08lx from "
471 "process %s / %d\n",
472 rp->p_delivermsg_vir,
473 rp->p_name,
474 rp->p_endpoint);
475 r = EFAULT;
478 /* Indicate message has been delivered; address is 'used'. */
479 rp->p_delivermsg.m_source = NONE;
480 rp->p_misc_flags &= ~MF_DELIVERMSG;
482 if(!(rp->p_misc_flags & MF_CONTEXT_SET)) {
483 rp->p_reg.retreg = r;
487 #if 0
488 static char *flagstr(u32_t e, const int dir)
490 static char str[80];
491 strcpy(str, "");
492 FLAG(I386_VM_PRESENT);
493 FLAG(I386_VM_WRITE);
494 FLAG(I386_VM_USER);
495 FLAG(I386_VM_PWT);
496 FLAG(I386_VM_PCD);
497 FLAG(I386_VM_GLOBAL);
498 if(dir)
499 FLAG(I386_VM_BIGPAGE); /* Page directory entry only */
500 else
501 FLAG(I386_VM_DIRTY); /* Page table entry only */
502 return str;
505 static void vm_pt_print(u32_t *pagetable, const u32_t v)
507 int pte;
508 int col = 0;
510 assert(!((u32_t) pagetable % I386_PAGE_SIZE));
512 for(pte = 0; pte < I386_VM_PT_ENTRIES; pte++) {
513 u32_t pte_v, pfa;
514 pte_v = phys_get32((u32_t) (pagetable + pte));
515 if(!(pte_v & I386_VM_PRESENT))
516 continue;
517 pfa = I386_VM_PFA(pte_v);
518 printf("%4d:%08lx:%08lx %2s ",
519 pte, v + I386_PAGE_SIZE*pte, pfa,
520 (pte_v & I386_VM_WRITE) ? "rw":"RO");
521 col++;
522 if(col == 3) { printf("\n"); col = 0; }
524 if(col > 0) printf("\n");
526 return;
529 static void vm_print(u32_t *root)
531 int pde;
533 assert(!((u32_t) root % I386_PAGE_SIZE));
535 printf("page table 0x%lx:\n", root);
537 for(pde = 0; pde < I386_VM_DIR_ENTRIES; pde++) {
538 u32_t pde_v;
539 u32_t *pte_a;
540 pde_v = phys_get32((u32_t) (root + pde));
541 if(!(pde_v & I386_VM_PRESENT))
542 continue;
543 if(pde_v & I386_VM_BIGPAGE) {
544 printf("%4d: 0x%lx, flags %s\n",
545 pde, I386_VM_PFA(pde_v), flagstr(pde_v, 1));
546 } else {
547 pte_a = (u32_t *) I386_VM_PFA(pde_v);
548 printf("%4d: pt %08lx %s\n",
549 pde, pte_a, flagstr(pde_v, 1));
550 vm_pt_print(pte_a, pde * I386_VM_PT_ENTRIES * I386_PAGE_SIZE);
551 printf("\n");
556 return;
558 #endif
560 int vm_memset(endpoint_t who, phys_bytes ph, const u8_t c, phys_bytes bytes)
562 u32_t p;
563 struct proc *whoptr = NULL;
565 /* NONE for physical, otherwise virtual */
566 if(who != NONE) {
567 int n;
568 if(!isokendpt(who, &n)) return ESRCH;
569 whoptr = proc_addr(n);
572 p = c | (c << 8) | (c << 16) | (c << 24);
574 assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v);
576 assert(!catch_pagefaults);
577 catch_pagefaults=1;
579 /* With VM, we have to map in the memory (virtual or physical).
580 * We can do this 4MB at a time.
582 while(bytes > 0) {
583 int changed = 0;
584 phys_bytes chunk = bytes, ptr, pfa;
585 ptr = createpde(whoptr, ph, &chunk, 0, &changed);
586 if(changed)
587 reload_cr3();
589 /* We can memset as many bytes as we have remaining,
590 * or as many as remain in the 4MB chunk we mapped in.
592 if((pfa=phys_memset(ptr, p, chunk))) {
593 printf("kernel memset pagefault\n");
594 break;
596 bytes -= chunk;
597 ph += chunk;
600 assert(catch_pagefaults);
601 catch_pagefaults=0;
603 assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v);
605 return OK;
608 /*===========================================================================*
609 * virtual_copy_f *
610 *===========================================================================*/
611 int virtual_copy_f(caller, src_addr, dst_addr, bytes, vmcheck)
612 struct proc * caller;
613 struct vir_addr *src_addr; /* source virtual address */
614 struct vir_addr *dst_addr; /* destination virtual address */
615 vir_bytes bytes; /* # of bytes to copy */
616 int vmcheck; /* if nonzero, can return VMSUSPEND */
618 /* Copy bytes from virtual address src_addr to virtual address dst_addr. */
619 struct vir_addr *vir_addr[2]; /* virtual source and destination address */
620 int i, r;
621 struct proc *procs[2];
623 assert((vmcheck && caller) || (!vmcheck && !caller));
625 /* Check copy count. */
626 if (bytes <= 0) return(EDOM);
628 /* Do some more checks and map virtual addresses to physical addresses. */
629 vir_addr[_SRC_] = src_addr;
630 vir_addr[_DST_] = dst_addr;
632 for (i=_SRC_; i<=_DST_; i++) {
633 endpoint_t proc_e = vir_addr[i]->proc_nr_e;
634 int proc_nr;
635 struct proc *p;
637 if(proc_e == NONE) {
638 p = NULL;
639 } else {
640 if(!isokendpt(proc_e, &proc_nr)) {
641 printf("virtual_copy: no reasonable endpoint\n");
642 return ESRCH;
644 p = proc_addr(proc_nr);
647 procs[i] = p;
650 if(caller && (caller->p_misc_flags & MF_KCALL_RESUME)) {
651 assert(caller->p_vmrequest.vmresult != VMSUSPEND);
652 if(caller->p_vmrequest.vmresult != OK) {
653 return caller->p_vmrequest.vmresult;
657 if((r=lin_lin_copy(procs[_SRC_], vir_addr[_SRC_]->offset,
658 procs[_DST_], vir_addr[_DST_]->offset, bytes)) != OK) {
659 struct proc *target = NULL;
660 phys_bytes lin;
661 if(r != EFAULT_SRC && r != EFAULT_DST)
662 panic("lin_lin_copy failed: %d", r);
663 if(!vmcheck || !caller) {
664 return r;
667 if(r == EFAULT_SRC) {
668 lin = vir_addr[_SRC_]->offset;
669 target = procs[_SRC_];
670 } else if(r == EFAULT_DST) {
671 lin = vir_addr[_DST_]->offset;
672 target = procs[_DST_];
673 } else {
674 panic("r strange: %d", r);
677 assert(caller);
678 assert(target);
680 vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL);
681 return VMSUSPEND;
684 return OK;
687 /*===========================================================================*
688 * data_copy *
689 *===========================================================================*/
690 int data_copy(const endpoint_t from_proc, const vir_bytes from_addr,
691 const endpoint_t to_proc, const vir_bytes to_addr,
692 size_t bytes)
694 struct vir_addr src, dst;
696 src.offset = from_addr;
697 dst.offset = to_addr;
698 src.proc_nr_e = from_proc;
699 dst.proc_nr_e = to_proc;
700 assert(src.proc_nr_e != NONE);
701 assert(dst.proc_nr_e != NONE);
703 return virtual_copy(&src, &dst, bytes);
706 /*===========================================================================*
707 * data_copy_vmcheck *
708 *===========================================================================*/
709 int data_copy_vmcheck(struct proc * caller,
710 const endpoint_t from_proc, const vir_bytes from_addr,
711 const endpoint_t to_proc, const vir_bytes to_addr,
712 size_t bytes)
714 struct vir_addr src, dst;
716 src.offset = from_addr;
717 dst.offset = to_addr;
718 src.proc_nr_e = from_proc;
719 dst.proc_nr_e = to_proc;
720 assert(src.proc_nr_e != NONE);
721 assert(dst.proc_nr_e != NONE);
723 return virtual_copy_vmcheck(caller, &src, &dst, bytes);
726 void memory_init(void)
728 assert(nfreepdes == 0);
730 freepdes[nfreepdes++] = kinfo.freepde_start++;
731 freepdes[nfreepdes++] = kinfo.freepde_start++;
733 assert(kinfo.freepde_start < I386_VM_DIR_ENTRIES);
734 assert(nfreepdes == 2);
735 assert(nfreepdes <= MAXFREEPDES);
738 /*===========================================================================*
739 * arch_proc_init *
740 *===========================================================================*/
741 void arch_proc_init(struct proc *pr, const u32_t ip, const u32_t sp, char *name)
743 arch_proc_reset(pr);
744 strlcpy(pr->p_name, name, sizeof(pr->p_name));
746 /* set custom state we know */
747 pr->p_reg.pc = ip;
748 pr->p_reg.sp = sp;
751 static int oxpcie_mapping_index = -1,
752 lapic_mapping_index = -1,
753 ioapic_first_index = -1,
754 ioapic_last_index = -1,
755 video_mem_mapping_index = -1,
756 usermapped_glo_index = -1,
757 usermapped_index = -1, first_um_idx = -1;
759 extern char *video_mem;
761 extern char usermapped_start, usermapped_end, usermapped_nonglo_start;
763 int arch_phys_map(const int index,
764 phys_bytes *addr,
765 phys_bytes *len,
766 int *flags)
768 static int first = 1;
769 int freeidx = 0;
770 static char *ser_var = NULL;
771 u32_t glo_len = (u32_t) &usermapped_nonglo_start -
772 (u32_t) &usermapped_start;
774 if(first) {
775 video_mem_mapping_index = freeidx++;
776 if(glo_len > 0) {
777 usermapped_glo_index = freeidx++;
780 usermapped_index = freeidx++;
781 first_um_idx = usermapped_index;
782 if(usermapped_glo_index != -1)
783 first_um_idx = usermapped_glo_index;
785 #ifdef USE_APIC
786 if(lapic_addr)
787 lapic_mapping_index = freeidx++;
788 if (ioapic_enabled) {
789 ioapic_first_index = freeidx;
790 assert(nioapics > 0);
791 freeidx += nioapics;
792 ioapic_last_index = freeidx-1;
794 #endif
796 #ifdef CONFIG_OXPCIE
797 if((ser_var = env_get("oxpcie"))) {
798 if(ser_var[0] != '0' || ser_var[1] != 'x') {
799 printf("oxpcie address in hex please\n");
800 } else {
801 printf("oxpcie address is %s\n", ser_var);
802 oxpcie_mapping_index = freeidx++;
805 #endif
807 first = 0;
810 if(index == usermapped_glo_index) {
811 *addr = vir2phys(&usermapped_start);
812 *len = glo_len;
813 *flags = VMMF_USER | VMMF_GLO;
814 return OK;
816 else if(index == usermapped_index) {
817 *addr = vir2phys(&usermapped_nonglo_start);
818 *len = (u32_t) &usermapped_end -
819 (u32_t) &usermapped_nonglo_start;
820 *flags = VMMF_USER;
821 return OK;
823 else if (index == video_mem_mapping_index) {
824 /* map video memory in so we can print panic messages */
825 *addr = MULTIBOOT_VIDEO_BUFFER;
826 *len = I386_PAGE_SIZE;
827 *flags = VMMF_WRITE;
828 return OK;
830 #ifdef USE_APIC
831 else if (index == lapic_mapping_index) {
832 /* map the local APIC if enabled */
833 if (!lapic_addr)
834 return EINVAL;
835 *addr = lapic_addr;
836 *len = 4 << 10 /* 4kB */;
837 *flags = VMMF_UNCACHED | VMMF_WRITE;
838 return OK;
840 else if (ioapic_enabled && index >= ioapic_first_index && index <= ioapic_last_index) {
841 int ioapic_idx = index - ioapic_first_index;
842 *addr = io_apic[ioapic_idx].paddr;
843 assert(*addr);
844 *len = 4 << 10 /* 4kB */;
845 *flags = VMMF_UNCACHED | VMMF_WRITE;
846 printf("ioapic map: addr 0x%lx\n", *addr);
847 return OK;
849 #endif
851 #if CONFIG_OXPCIE
852 if(index == oxpcie_mapping_index) {
853 *addr = strtoul(ser_var+2, NULL, 16);
854 *len = 0x4000;
855 *flags = VMMF_UNCACHED | VMMF_WRITE;
856 return OK;
858 #endif
860 return EINVAL;
863 int arch_phys_map_reply(const int index, const vir_bytes addr)
865 #ifdef USE_APIC
866 /* if local APIC is enabled */
867 if (index == lapic_mapping_index && lapic_addr) {
868 lapic_addr_vaddr = addr;
869 return OK;
871 else if (ioapic_enabled && index >= ioapic_first_index &&
872 index <= ioapic_last_index) {
873 int i = index - ioapic_first_index;
874 io_apic[i].vaddr = addr;
875 return OK;
877 #endif
879 #if CONFIG_OXPCIE
880 if (index == oxpcie_mapping_index) {
881 oxpcie_set_vaddr((unsigned char *) addr);
882 return OK;
884 #endif
885 if(index == first_um_idx) {
886 u32_t usermapped_offset;
887 assert(addr > (u32_t) &usermapped_start);
888 usermapped_offset = addr - (u32_t) &usermapped_start;
889 memset(&minix_kerninfo, 0, sizeof(minix_kerninfo));
890 #define FIXEDPTR(ptr) (void *) ((u32_t)ptr + usermapped_offset)
891 #define FIXPTR(ptr) ptr = FIXEDPTR(ptr)
892 #define ASSIGN(minixstruct) minix_kerninfo.minixstruct = FIXEDPTR(&minixstruct)
893 ASSIGN(kinfo);
894 ASSIGN(machine);
895 ASSIGN(kmessages);
896 ASSIGN(loadinfo);
898 /* adjust the pointers of the functions and the struct
899 * itself to the user-accessible mapping
901 minix_kerninfo.kerninfo_magic = KERNINFO_MAGIC;
902 minix_kerninfo.minix_feature_flags = minix_feature_flags;
903 minix_kerninfo_user = (vir_bytes) FIXEDPTR(&minix_kerninfo);
905 return OK;
908 if(index == usermapped_index) return OK;
910 if (index == video_mem_mapping_index) {
911 video_mem_vaddr = addr;
912 return OK;
915 return EINVAL;
918 int arch_enable_paging(struct proc * caller)
920 assert(caller->p_seg.p_cr3);
922 /* load caller's page table */
923 switch_address_space(caller);
925 video_mem = (char *) video_mem_vaddr;
927 #ifdef USE_APIC
928 /* start using the virtual addresses */
930 /* if local APIC is enabled */
931 if (lapic_addr) {
932 lapic_addr = lapic_addr_vaddr;
933 lapic_eoi_addr = LAPIC_EOI;
935 /* if IO apics are enabled */
936 if (ioapic_enabled) {
937 int i;
939 for (i = 0; i < nioapics; i++) {
940 io_apic[i].addr = io_apic[i].vaddr;
943 #if CONFIG_SMP
944 barrier();
946 wait_for_APs_to_finish_booting();
947 #endif
948 #endif
950 #ifdef USE_WATCHDOG
952 * We make sure that we don't enable the watchdog until paging is turned
953 * on as we might get an NMI while switching and we might still use wrong
954 * lapic address. Bad things would happen. It is unfortunate but such is
955 * life
957 if (watchdog_enabled)
958 i386_watchdog_start();
959 #endif
961 return OK;
964 void release_address_space(struct proc *pr)
966 pr->p_seg.p_cr3_v = NULL;
969 /* computes a checksum of a buffer of a given length. The byte sum must be zero */
970 int platform_tbl_checksum_ok(void *ptr, unsigned int length)
972 u8_t total = 0;
973 unsigned int i;
974 for (i = 0; i < length; i++)
975 total += ((unsigned char *)ptr)[i];
976 return !total;
979 int platform_tbl_ptr(phys_bytes start,
980 phys_bytes end,
981 unsigned increment,
982 void * buff,
983 unsigned size,
984 phys_bytes * phys_addr,
985 int ((* cmp_f)(void *)))
987 phys_bytes addr;
989 for (addr = start; addr < end; addr += increment) {
990 phys_copy (addr, (phys_bytes) buff, size);
991 if (cmp_f(buff)) {
992 if (phys_addr)
993 *phys_addr = addr;
994 return 1;
997 return 0;