+/rtos

git clone https://git.julia.blue/+/rtos

Vivek MalneediMerge branch 'master' of github.com:EE445M-Sp20/lab-6-vm22967_jbr2558ee4080a

master
4.7 KiB185 linesraw
1// Based on loader by Martin Ribelotta and Andreas Gerstlauer
2
3#include "OS.h"
4#include "heap.h"
5#include "io.h"
6#include "littlefs.h"
7#include "printf.h"
8#include "std.h"
9#include <stddef.h>
10
11const uint32_t user_process_stack = 1024;
12
13#define ERR(msg) puts(RED "ELF ERROR: " NORMAL msg)
14#define SEGMENT_OFFSET(e, n) (e->programHeaderTable + n * sizeof(ProgramHeader))
15
16typedef struct {
17    char ident[16];
18    uint16_t type;
19    uint16_t machine;
20    uint32_t version;
21    uint32_t entry;
22    uint32_t phoff;
23    uint32_t shoff;
24    uint32_t flags;
25    uint16_t ehsize;
26    uint16_t phentsize;
27    uint16_t phnum;
28    uint16_t shentsize;
29    uint16_t shnum;
30    uint16_t shstrndx;
31} Header;
32
33typedef struct {
34    void* data;
35    int segIdx;
36} Segment;
37
38typedef struct {
39    uint32_t type;
40    uint32_t offset;
41    uint32_t vaddr;
42    uint32_t paddr;
43    uint32_t filesz;
44    uint32_t memsz;
45    uint32_t flags;
46    uint32_t align;
47} ProgramHeader;
48
49typedef struct {
50    size_t type;
51    uint32_t entry;
52
53    size_t segments;
54    uint32_t programHeaderTable;
55
56    Segment load_text;
57    Segment load_data;
58} Executable;
59
60static void free_segment(Segment* s) {
61    if (s->data)
62        free(s->data);
63}
64
65static bool load_segment(Executable* e, Segment* s, ProgramHeader* h) {
66    if (!h->memsz) {
67        printf(" No data for section");
68        return true;
69    }
70    s->data = malloc(h->memsz);
71    if (!s->data) {
72        ERR("    GET MEMORY fail");
73        return false;
74    }
75    if (!littlefs_seek(h->offset)) {
76        ERR("    seek fail");
77        free_segment(s);
78        return false;
79    }
80    if (littlefs_read_buffer(s->data, h->filesz) != h->filesz) {
81        ERR("     read data fail");
82        return false;
83    }
84    if (h->memsz > h->filesz) {
85        memset((uint8_t*)s->data + h->filesz, 0, h->memsz - h->filesz);
86    }
87    return true;
88}
89
90static bool read_segment_header(Executable* e, int n, ProgramHeader* h) {
91    uint32_t offset = SEGMENT_OFFSET(e, n);
92    return littlefs_seek(offset) &&
93           littlefs_read_buffer(h, sizeof(ProgramHeader)) ==
94               sizeof(ProgramHeader);
95}
96
97static bool init_elf(Executable* e) {
98    memset(e, 0, sizeof(Executable));
99
100    Header h;
101    if (littlefs_read_buffer(&h, sizeof(h)) != sizeof(h)) {
102        return false;
103    }
104
105    e->entry = h.entry;
106    e->type = h.type;
107    e->segments = h.phnum;
108    e->programHeaderTable = h.phoff;
109
110    // TODO: Check that it's actualy an ELF using magic numbers
111    return true;
112}
113
114static bool jump_to(uint32_t ofs, void* text, void* data) {
115    if (ofs) {
116        return OS_AddProcess((void (*)(void))((uint8_t*)text + ofs), text, data,
117                             user_process_stack, 0);
118    } else {
119        ERR("No entry defined.");
120        return false;
121    }
122}
123
124// CURSED LINKER HACK (see /userprog/user.ld for details)
125void fix_GOT(Segment* data) {
126    uint32_t* start = data->data;
127    uint32_t* current = start;
128    uint32_t offset = *current++;
129    while (*current != 0xAAAAAAAA) { *current++ += (uint32_t)start - offset; }
130}
131
132bool exec_elf(const char* path) {
133    static Executable exec; // this is only static to save stack space
134    if (!(littlefs_open_file(path, false) && init_elf(&exec))) {
135        printf("Invalid elf %s\n\r", path);
136        return false;
137    }
138    if (exec.type != 2) { // 2 == EXEC
139        ERR("Not of type EXEC");
140        return false;
141    }
142
143    bool found_writable = false, found_executable = false;
144    for (int n = 0; n < exec.segments; n++) {
145        ProgramHeader ph;
146        if (!read_segment_header(&exec, n, &ph)) {
147            ERR("Failed to read segment");
148            return false;
149        }
150        if (ph.type != 1) { // 1 == LOAD
151            printf("Skipping segment %d\n\r", n);
152            continue;
153        }
154        printf("Examining segment %d\n\r", n);
155
156        if (ph.flags & 2) { // 2 == W
157            if (!load_segment(&exec, &exec.load_data, &ph)) {
158                return false;
159            }
160            exec.load_data.segIdx = n;
161            if (found_writable) {
162                ERR("Can't handle multiple writable segments");
163                return false;
164            }
165            found_writable = true;
166            fix_GOT(&exec.load_data);
167        } else if (ph.flags & 1) { // 1 == X
168            if (!load_segment(&exec, &exec.load_text, &ph)) {
169                return false;
170            }
171            exec.load_text.segIdx = n;
172            if (found_executable) {
173                ERR("Can't handle multiple executable segments");
174                return false;
175            }
176            found_executable = true;
177        } else {
178            ERR("Section wasn't writable or executable");
179            return false;
180        }
181    }
182    bool ret = jump_to(exec.entry, exec.load_text.data, exec.load_data.data);
183    littlefs_close_file();
184    return ret;
185}