A Total Overhaul on the Lunaix's Virtual Memory Model (#26)
[lunaix-os.git] / lunaix-os / kernel / exe / elf32 / ldelf32.c
1 #include <lunaix/exebi/elf32.h>
2 #include <lunaix/load.h>
3 #include <lunaix/mm/mmap.h>
4 #include <lunaix/mm/valloc.h>
5 #include <lunaix/spike.h>
6
7 #include <sys/mm/mempart.h>
8
9 int
10 elf32_smap(struct load_context* ldctx,
11            const struct elf32* elf,
12            struct elf32_phdr* phdre,
13            uintptr_t base_va)
14 {
15     struct v_file* elfile = (struct v_file*)elf->elf_file;
16
17     assert(!va_offset(phdre->p_offset));
18
19     int proct = 0;
20     if ((phdre->p_flags & PF_R)) {
21         proct |= PROT_READ;
22     }
23     if ((phdre->p_flags & PF_W)) {
24         proct |= PROT_WRITE;
25     }
26     if ((phdre->p_flags & PF_X)) {
27         proct |= PROT_EXEC;
28     }
29
30     uintptr_t va = phdre->p_va + base_va;
31     struct exec_container* container = ldctx->container;
32     struct mmap_param param = { .vms_mnt = container->vms_mnt,
33                                 .pvms = vmspace(container->proc),
34                                 .proct = proct,
35                                 .offset = va_align(phdre->p_offset),
36                                 .mlen = va_alignup(phdre->p_memsz),
37                                 .flags = MAP_FIXED | MAP_PRIVATE,
38                                 .type = REGION_TYPE_CODE };
39
40     struct mm_region* seg_reg;
41     int status = mmap_user(NULL, &seg_reg, va_align(va), elfile, &param);
42
43     if (!status) {
44         size_t next_addr = phdre->p_memsz + va;
45         ldctx->end = MAX(ldctx->end, va_alignup(next_addr));
46         ldctx->mem_sz += phdre->p_memsz;
47     } else {
48         // we probably fucked up our process
49         terminate_current(-1);
50     }
51
52     return status;
53 }
54
55 int
56 load_executable(struct load_context* context, const struct v_file* exefile)
57 {
58     int errno = 0;
59
60     char* ldpath = NULL;
61     struct elf32 elf;
62     struct exec_container* container = context->container;
63
64     if ((errno = elf32_openat(&elf, exefile))) {
65         goto done;
66     }
67
68     if (!elf32_check_arch(&elf)) {
69         errno = EINVAL;
70         goto done;
71     }
72
73     if (!(elf32_check_exec(&elf, ET_EXEC) || elf32_check_exec(&elf, ET_DYN))) {
74         errno = ENOEXEC;
75         goto done;
76     }
77
78     ldpath = valloc(256);
79     errno = elf32_find_loader(&elf, ldpath, 256);
80     uintptr_t load_base = 0;
81
82     if (errno < 0) {
83         goto done;
84     }
85
86     if (errno != NO_LOADER) {
87         container->argv_pp[1] = ldpath;
88
89         // close old elf
90         if ((errno = elf32_close(&elf))) {
91             goto done;
92         }
93
94         // open the loader instead
95         if ((errno = elf32_open(&elf, ldpath))) {
96             goto done;
97         }
98
99         // Is this the valid loader?
100         if (!elf32_static_linked(&elf) || !elf32_check_exec(&elf, ET_DYN)) {
101             errno = ELIBBAD;
102             goto done_close_elf32;
103         }
104
105         load_base = USR_MMAP;
106     }
107
108     context->entry = elf.eheader.e_entry + load_base;
109
110     struct v_file* elfile = (struct v_file*)elf.elf_file;
111
112     for (size_t i = 0; i < elf.eheader.e_phnum && !errno; i++) {
113         struct elf32_phdr* phdr = &elf.pheaders[i];
114
115         if (phdr->p_type != PT_LOAD) {
116             continue;
117         }
118
119         if (phdr->p_align != PAGE_SIZE) {
120             // surprising alignment!
121             errno = ENOEXEC;
122             break;
123         }
124
125         errno = elf32_smap(context, &elf, phdr, load_base);
126     }
127
128 done_close_elf32:
129     elf32_close(&elf);
130
131 done:
132     if (!container->argv_pp[1]) {
133         vfree_safe(ldpath);
134     }
135     return errno;
136 }