AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
9
10#include <core/printk.h>
11#include <core/panic.h>
12#include <core/string.h>
13#include <core/module.h>
14#include <core/kargs.h>
15#include <core/arch.h>
16#include <mm/mm.h>
17#include <mm/pmap.h>
18#include <mm/vm.h>
19#include <dev/dev.h>
20#include <fs/vfs.h>
21#include <fs/initramfs.h>
22#include <sys/proc.h>
23#include <sys/sched.h>
24#include <sys/binfmt.h>
25#include <boot/boot.h>
26#include <console/earlycon.h>
27
28#include <ds/hashmap.h>
29
38void kmain(struct boot *boot)
39{
40 /* check the kernel's allocator, if asked to (tests/kvmem) */
41 if (!kargs_get("kvmem.test", NULL))
43
44 if (!kargs_get("kvmem.guard", NULL))
46
47 kdev_init();
48 vfs_init();
50
51 /* mount the root filesystem: a disk partition (root=/dev/hda2), or
52 * the RAM disk that the boot loader loaded */
53 const char *root = NULL, *rootfstype = NULL;
54 int err;
55
56 if (!kargs_get("root", &root) && root) {
57 kargs_get("rootfstype", &rootfstype);
58
59 if ((err = vfs_mount_root_dev(root, rootfstype))) {
60 printk("kernel: can't mount %s: error %d\n", root, -err);
61 panic("could not mount the root filesystem");
62 }
63 } else if (boot->modules_count) {
65 } else {
66 panic("No modules loaded: unable to load ramdisk");
67 }
68
69 /* mount devfs on /dev, with /dev/console, so that init can write to
70 * the console right away */
71 struct uio uio = {.mask = 022};
72
73 if (!vfs_mount("devfs", "/dev", 0, NULL, &uio))
74 vfs_mknod("/dev/console", S_IFCHR | 0600, DEV(5, 1), &uio, NULL);
75
76 printk("kernel: loading init process\n");
77
78 struct proc *init;
79
80 if ((err = proc_new(&init))) {
81 panic("failed to allocate process structure for init");
82 }
83
84 initproc = init;
85
86 curthread = (struct thread *) init->threads.head->value;
87 curproc = init;
88
90
91 const char *init_p = "/init";
92
93 if ((err = binfmt_load(init, init_p, &init))) {
94 printk("kernel: failed to load %s: error: %d\n", init_p, -err);
95 panic("could not load init process");
96 }
97
98 arch_proc_init(init);
99
100 /* init's argument is the module's command line (none when booting
101 * from a disk) */
102 char *cmdline = boot->modules_count ? boot->modules[0].cmdline : "";
103 char *argp[] = {cmdline, 0};
104 char *envp[] = {0};
105
106 init_execve(init, 1, argp, 0, envp);
107
108#if EARLYCON_DISABLE_ON_INIT
110#endif
111
112 sched_init_spawn(init);
113
114 panic("Scheduler failed to spawn init");
115}
void arch_proc_init(struct proc *proc)
Sets up the architecture's part of a new process's thread (init's, at boot): its kernel stack,...
Definition proc.c:19
#define panic(s)
Definition panic.h:15
int binfmt_load(struct proc *proc, const char *path, struct proc **ref)
Loads a program into a process, for execve().
Definition binfmt.c:124
#define DEV(major, minor)
Definition dev.h:76
int load_ramdisk(module_t *module)
Mounts the RAM disk as the root filesystem.
Definition initramfs.c:49
int kargs_get(const char *key, const char **value)
Looks up an argument of the kernel's command line.
Definition kargs.c:73
void kdev_init()
Starts the device layer, at boot.
Definition kdev.c:615
void kmain(struct boot *boot)
Starts the kernel's generic part, once the architecture has set up the CPU, memory and the platform.
Definition main.c:38
int vfs_mknod(const char *path, mode_t mode, dev_t dev, struct uio *uio, struct vnode **ref)
Makes a node at a path.
Definition mknod.c:21
int modules_init(void)
Runs the init function of every built-in module, at boot.
Definition module.c:21
int vfs_mount_root_dev(const char *name, const char *type)
Mounts the root filesystem from a disk partition (the root=/dev/hda2 kernel argument).
Definition mount.c:120
int vfs_mount(const char *type, const char *dir, int flags, void *data, struct uio *uio)
Mounts a filesystem of a type on a directory, for mount().
Definition mount.c:29
struct pmap * pmap_switch(struct pmap *pmap)
Switches this CPU to an address space's page tables.
Definition pmap.c:164
void earlycon_disable()
Stops sending the kernel's messages to the early console, once a real console has taken over.
Definition printk.c:34
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
Definition printk.c:249
int proc_new(struct proc **ref)
Makes a new process structure, with one thread and an empty address space, on the list of every proce...
Definition proc.c:114
void init_execve(struct proc *proc, int argc, char *const argv[], int envc, char *const envp[])
Sets up init, the first process, to start its program: its initial stack with its arguments and envir...
Definition execve.c:332
struct proc * initproc
Definition proc.c:39
void sched_init_spawn(struct proc *init)
Starts the scheduler with the first process, init, and runs it.
Definition sched.c:375
#define curproc
Definition sched.h:13
#define curthread
Definition sched.h:12
#define NULL
Definition stddef.h:27
Definition boot.h:35
module_t * modules
Definition boot.h:41
int modules_count
Definition boot.h:39
char * cmdline
Definition boot.h:18
A process.
Definition proc.h:75
struct vm_space vm_space
its address space
Definition proc.h:129
struct queue threads
its threads
Definition proc.h:136
void * value
Definition queue.h:20
struct qnode * head
Definition queue.h:31
A thread.
Definition thread.h:23
Who does a filesystem operation: where its paths start, and its credentials and umask.
Definition vfs.h:60
struct pmap * pmap
its page tables (the architecture's)
Definition vm.h:27
void vfs_init(void)
Sets up the VFS at boot.
Definition vfs.c:74
void kvmem_selftest(void)
Runs the allocator's self-test, for the kvmem.test kernel argument, and prints whether it passed.
Definition kvmem.c:264
void kvmem_guardtest(void)
Writes one byte past the end of an object, for the kvmem.guard kernel argument: the guard page should...
Definition kvmem.c:278