~cypheon/xos

xos/proc.c -rw-r--r-- 1.2 KiB
567db604 — Johann Rudloff Implement scanning and allocation of physical memory. 7 years ago
                                                                                
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
#include <sys/memory.h>
#include <sys/types.h>
#include <sys/paging.h>
#include <sys/proc.h>
#include <kern/scheduler.h>
#include <stdlib.h>

static int next_pid = 1;

struct proc *
proc_create(void) {
  struct proc *p = kmalloc(sizeof(struct proc));
  *p = (struct proc){
    .pid = next_pid++,
    .pd = pd_create(),
    .page_count = 0,
    .pcb = kmalloc(sizeof(struct pcb)),
    .kernel_stack = kmalloc(PAGE_SIZE),
    .tss = kmalloc(PAGE_SIZE),
  };

  return p;
}

void
proc_destroy(struct proc *p) {
  kfree(p->tss);
  kfree(p->pcb);
  kfree(p->kernel_stack);
  pd_destroy(p->pd);
  kfree(p);
}

void
copy_from_user(void *dst, uint32_t user_addr, size_t len) {
  if (user_addr / PAGE_SIZE != (user_addr + len) / PAGE_SIZE) {
    panic("copy_from_user: user memory area corsses page boundaries");
  }
  void *tmp_map = kmalloc_align(PAGE_SIZE, PAGE_SIZE);
  uint32_t phys_addr_orig = pd_get_phys(kernel_paging_directory, tmp_map);
  uint32_t phys_addr_user = pd_get_phys(current->pd, user_addr);
  pd_map_page(kernel_paging_directory, tmp_map, phys_addr_user, 0);
  memcpy(dst, tmp_map + phys_addr_user % PAGE_SIZE, len);

  /* restore original mapping */
  pd_map_page(kernel_paging_directory, tmp_map, phys_addr_orig, 0);
  kfree(tmp_map);
}