#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/kassert.h>
#include <sys/sysctl.h>
#include <sys/kmem.h>
#include <sys/queue.h>
#include <sys/disk.h>
#include <sys/elf64.h>
#include <machine/amd64.h>
#include <machine/trap.h>
#include <machine/pmap.h>
#include <sys/thread.h>
#include <sys/spinlock.h>
#include <sys/loader.h>
#include <sys/vfs.h>
Go to the source code of this file.
◆ Console_OpenHandle()
Handle * Console_OpenHandle |
( |
| ) |
|
Definition at line 200 of file console.c.
201{
202 Handle *handle = Handle_Alloc();
203 if (!handle)
205
210
211 return handle;
212}
int Console_Close(Handle *handle)
int Console_Write(Handle *handle, void *buf, uint64_t off, uint64_t len)
int Console_Read(Handle *handle, void *buf, uint64_t off, uint64_t len)
int Console_Flush(Handle *handle)
int(* write)(Handle *, void *, uint64_t, uint64_t)
int(* read)(Handle *, void *, uint64_t, uint64_t)
◆ Loader_CheckHeader()
Loader_CheckHeader –
Check that the program has a valid ELF header.
Definition at line 34 of file loader.c.
35{
40 return false;
41
43 return false;
44 }
45
47 return false;
48 }
49
50 return true;
51}
unsigned char e_ident[EI_NIDENT]
◆ Loader_Load()
Loader_Load –
Load the ELF binary into the process belonging to the thread.
Definition at line 131 of file loader.c.
132{
133 int i;
137
140
142 Log(loader,
"Not a valid executable!\n");
143 return false;
144 }
145
146 Log(loader,
"%8s %16s %8s %8s\n",
"Offset",
"VAddr",
"FileSize",
"MemSize");
147 for (i = 0; i < ehdr->
e_phnum; i++)
148 {
150 if (phdr[i].p_type ==
PT_LOAD) {
153 Log(loader,
"%08llx %016llx %08llx %08llx\n", phdr[i].p_offset,
154 phdr[i].p_vaddr, phdr[i].p_filesz, phdr[i].p_memsz);
155
156
159
160 Log(loader,
"AllocMap %016llx %08llx\n", va, memsz);
162
164 return false;
165 }
166 }
167 }
168
170
171
172
173
175
176 return true;
177}
#define Log(_module, _format,...)
bool Loader_CheckHeader(const Elf64_Ehdr *ehdr)
#define MEM_USERSPACE_STKLEN
#define MEM_USERSPACE_STKBASE
bool PMap_AllocMap(AS *as, uint64_t virt, uint64_t len, uint64_t flags)
◆ Loader_LoadInit()
Loader_LoadInit –
The init process is created from the execution kernel thread that initializes the system. This function initializes the thread and process state then loads the init binary.
Definition at line 187 of file loader.c.
188{
189 int status;
190 void *pg;
192
194 if (!pg)
195 Panic(
"Not enough memory!");
196
199 if (status < 0)
200 Panic(
"Loading init process failed!");
201 status =
VFS_Read(initvn, pg, 0, 1024);
202 if (status < 0)
203 Panic(
"Reading init process failed!");
204
206
207
214
215
216
217
219
221
222 Log(loader,
"Jumping to userspace\n");
223
224
225
226
228
229
230
231
233 ap[0] = 0;
234 ap[1] = 0;
235 ap[2] = 0xDEADBEEF;
237
239
240
241
242
243
245 memset(&tf, 0,
sizeof(tf));
254
255
256
257
258 Panic(
"Unreachable: Trap_Pop() returned!\n");
259}
int Copy_Out(void *fromkernel, uintptr_t touser, uintptr_t len)
uint64_t Handle_Add(Process *proc, Handle *handle)
void * PAlloc_AllocPage()
bool Loader_Load(Thread *thr, VNode *vn, void *buf, uint64_t len)
Handle * Console_OpenHandle()
#define MEM_USERSPACE_STKTOP
void PMap_LoadAS(AS *space)
void * memset(void *dst, int c, size_t len)
void Trap_Pop(TrapFrame *tf)
int VFS_Read(VNode *fn, void *buf, uint64_t off, uint64_t len)
VNode * VFS_Lookup(const char *path)
void Panic(const char *str)
◆ LoaderLoadSegment()
LoaderLoadSegment –
Loads a single segment into the target address space. This function loads a single page at a time because it has to lookup the address mappings through the page tables.
Definition at line 61 of file loader.c.
63{
64 void *raddr;
65
66 if ((vaddr %
PGSIZE) != 0) {
69
72 vaddr += rlen;
73 offset += rlen;
75 }
76
83 }
84
88 }
89}
uintptr_t PMap_Translate(AS *space, uintptr_t va)
◆ LoaderZeroSegment()
LoaderZeroSegment –
Zeroes a segment of memory in the target address space. This is done one page a time while translating the virtual address to physical.
Definition at line 98 of file loader.c.
99{
100 void *raddr;
101
102 if ((vaddr %
PGSIZE) != 0) {
105
108 vaddr += rlen;
110 }
111
117 }
118
122 }
123}