feat: heap support and re-worked
[lunaix-os.git] / lunaix-os / kernel / loader / elf.c
1 #include <lunaix/common.h>
2 #include <lunaix/elf.h>
3 #include <lunaix/fs.h>
4 #include <lunaix/ld.h>
5 #include <lunaix/mm/mmap.h>
6 #include <lunaix/mm/valloc.h>
7 #include <lunaix/mm/vmm.h>
8 #include <lunaix/spike.h>
9
10 int
11 __elf_populate_mapped(struct mm_region* region, void* pg, off_t offset)
12 {
13     size_t segsz = region->flen;
14     size_t segoff = offset - region->foff;
15
16     if (segoff >= segsz) {
17         return 0;
18     }
19
20     struct v_file* file = region->mfile;
21     size_t rdlen = MIN(segsz - segoff, PG_SIZE);
22
23     if (rdlen == PG_SIZE) {
24         // This is because we want to exploit any optimization on read_page
25         return file->ops->read_page(file->inode, pg, PG_SIZE, offset);
26     } else {
27         return file->ops->read(file->inode, pg, rdlen, offset);
28     }
29 }
30
31 int
32 elf_map_segment(struct ld_param* ldparam,
33                 struct v_file* elfile,
34                 struct elf32_phdr* phdr)
35 {
36     int proct = 0;
37     if ((phdr->p_flags & PF_R)) {
38         proct |= PROT_READ;
39     }
40     if ((phdr->p_flags & PF_W)) {
41         proct |= PROT_WRITE;
42     }
43     if ((phdr->p_flags & PF_X)) {
44         proct |= PROT_EXEC;
45     }
46
47     struct mm_region* seg_reg;
48     struct mmap_param param = { .vms_mnt = ldparam->vms_mnt,
49                                 .pvms = &ldparam->proc->mm,
50                                 .proct = proct,
51                                 .offset = phdr->p_offset,
52                                 .mlen = ROUNDUP(phdr->p_memsz, PG_SIZE),
53                                 .flen = phdr->p_filesz,
54                                 .flags = MAP_FIXED | MAP_PRIVATE,
55                                 .type = REGION_TYPE_CODE };
56
57     int status = mem_map(NULL, &seg_reg, PG_ALIGN(phdr->p_va), elfile, &param);
58
59     if (!status) {
60         seg_reg->init_page = __elf_populate_mapped;
61
62         size_t next_addr = phdr->p_memsz + phdr->p_va;
63         ldparam->info.end = MAX(ldparam->info.end, ROUNDUP(next_addr, PG_SIZE));
64         ldparam->info.mem_sz += phdr->p_memsz;
65     }
66
67     return status;
68 }
69
70 int
71 elf_setup_mapping(struct ld_param* ldparam,
72                   struct v_file* elfile,
73                   struct elf32_ehdr* ehdr)
74 {
75     int status = 0;
76     size_t tbl_sz = ehdr->e_phnum * SIZE_PHDR;
77     struct elf32_phdr* phdrs = valloc(tbl_sz);
78
79     if (!phdrs) {
80         status = ENOMEM;
81         goto done;
82     }
83
84     tbl_sz = 1 << ILOG2(tbl_sz);
85     phdrs = elfile->ops->read(elfile->inode, phdrs, tbl_sz, ehdr->e_phoff);
86
87     if (PG_ALIGN(phdrs[0].p_va) != USER_START) {
88         status = ENOEXEC;
89         goto done;
90     }
91
92     size_t entries = tbl_sz / SIZE_PHDR;
93     for (size_t i = 0; i < entries; i++) {
94         struct elf32_phdr* phdr = &phdrs[i];
95
96         if (phdr->p_type == PT_LOAD) {
97             if (phdr->p_align == PG_SIZE) {
98                 status = elf_map_segment(ldparam, elfile, phdr);
99             } else {
100                 // surprising alignment!
101                 status = ENOEXEC;
102             }
103         }
104         // TODO process other types of segments
105
106         if (status) {
107             // errno in the middle of mapping restructuring, it is impossible
108             // to recover!
109             ldparam->status |= LD_STAT_FKUP;
110             goto done;
111         }
112     }
113
114 done:
115     vfree(phdrs);
116     return status;
117 }
118
119 int
120 elf_load(struct ld_param* ldparam, struct v_file* elfile)
121 {
122     struct elf32_ehdr* ehdr = valloc(SIZE_EHDR);
123     int status = elfile->ops->read(elfile->inode, ehdr, SIZE_EHDR, 0);
124
125     if (status) {
126         goto done;
127     }
128
129     if (!elf_check_exec(ehdr)) {
130         status = ENOEXEC;
131         goto done;
132     }
133
134     if ((status = elf_setup_mapping(ldparam, elfile, ehdr))) {
135         goto done;
136     }
137
138     ldparam->info.ehdr_out = *ehdr;
139
140 done:
141     vfree(ehdr);
142     return status;
143 }