Skip to content

Commit f7bbaad

Browse files
committed
sched: create pid1 and load bash
1 parent 206aa39 commit f7bbaad

File tree

5 files changed

+118
-68
lines changed

5 files changed

+118
-68
lines changed

kernel/interfaces/system/proc/cred.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export namespace sched
122122

123123
struct cred_t
124124
{
125-
uid_t ruid = 0; // real
125+
uid_t ruid = 0; // real
126126
uid_t euid = 0; // effective
127127
uid_t suid = 0; // saved
128128
uid_t fsuid = 0; // filesystem

kernel/interfaces/system/proc/scheduler.cppm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export namespace sched
104104
// called on yield, block, timer or wake up
105105
void schedule();
106106

107+
process_t *create_process(process_t *parent);
108+
107109
// create a new kernel thread under pid 0
108110
thread_t *create_kthread(std::uintptr_t ip, std::uintptr_t arg, nice_t nice = default_nice);
109111

kernel/source/arch/x86_64/system/sched/sched.S

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,10 @@ sched_context_switch:
6767
sched_return_to_user:
6868
push 0x23 # ss
6969
push rsi # rsp
70-
71-
# rflags
72-
# TODO-SCHED-REWRITE
73-
pushfq
74-
or qword ptr [rsp], 0x200
75-
# push 0x202
76-
70+
pushfq # rflags
7771
push 0x2B # cs
7872
push rdi # rip
73+
74+
swapgs
7975
iretq
8076
.global sched_return_to_user

kernel/source/boot/kernel.cpp

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,66 +13,73 @@ void kthread()
1313
lib::initgraph::postsched_init_engine.run();
1414
pmm::reclaim_bootloader_memory();
1515

16-
// TODO-SCHED-REWRITE
17-
// sched::thread_t *thread = nullptr;
18-
// {
19-
// lib::path_view path { "/usr/bin/bash" };
20-
// lib::info("loading {}", path);
21-
22-
// auto ret = vfs::resolve(std::nullopt, path);
23-
// if (!ret.has_value())
24-
// lib::panic("could not resolve {}", path);
25-
26-
// auto res = vfs::reduce(ret->parent, ret->target);
27-
// if (!res.has_value())
28-
// lib::panic("could not reduce {}", path);
29-
30-
// auto file = vfs::file::create(res.value(), 0, 0, 0);
31-
// auto format = bin::exec::identify(file);
32-
// if (!format)
33-
// lib::panic("could not identify {} file format", path);
34-
35-
// auto pmap = std::make_shared<vmm::pagemap>();
36-
// auto proc = sched::create_pid1(std::move(pmap));
37-
38-
// proc->ruid = proc->euid = proc->suid = 1000;
39-
// proc->rgid = proc->egid = proc->sgid = 1000;
40-
41-
// lib::path_view tty_path { "/dev/ttyS0" };
42-
// ret = vfs::resolve(std::nullopt, tty_path);
43-
// if (!ret.has_value())
44-
// lib::panic("could not resolve {}", tty_path);
45-
// auto tty = vfs::filedesc::create(ret->target, vfs::o_rdwr, proc->pid);
46-
// if (!tty || !tty->file)
47-
// lib::panic("could not create {} filedesc", tty_path);
48-
// if (!tty->file->open(0))
49-
// lib::panic("could not open {}", tty_path);
50-
51-
// proc->fdt->allocate_fd(tty, 0, false);
52-
// proc->fdt->dup(0, 1, false, false);
53-
// proc->fdt->dup(0, 2, false, false);
54-
55-
// thread = format->load({
56-
// .pathname = path.data(),
57-
// .file = file,
58-
// .interp = { },
59-
// .argv = { path.basename().data() },
60-
// .envp = {
61-
// "TERM=linux",
62-
// "USER=ilobilix",
63-
// "HOME=/home/ilobilix",
64-
// "PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin"
65-
// }
66-
// }, proc);
67-
68-
// if (!thread)
69-
// lib::panic("could not create a thread for {}", path);
70-
71-
// thread->status = sched::status::ready;
72-
// }
16+
sched::thread_t *thread = nullptr;
17+
{
18+
lib::path_view path { "/usr/bin/bash" };
19+
lib::info("loading {}", path);
20+
21+
auto ret = vfs::resolve(std::nullopt, path);
22+
if (!ret.has_value())
23+
lib::panic("could not resolve {}", path);
24+
25+
auto res = vfs::reduce(ret->parent, ret->target);
26+
if (!res.has_value())
27+
lib::panic("could not reduce {}", path);
28+
29+
auto file = vfs::file::create(res.value(), 0, 0, 0);
30+
auto format = bin::exec::identify(file);
31+
if (!format)
32+
lib::panic("could not identify {} file format", path);
33+
34+
auto proc = sched::create_process(nullptr);
35+
36+
proc->vmspace = std::make_shared<vmm::vmspace>(
37+
std::make_shared<vmm::pagemap>()
38+
);
39+
40+
proc->vfs = std::make_shared<sched::process_t::vfs_state>();
41+
proc->vfs->root = vfs::get_root(true);
42+
proc->vfs->cwd = proc->vfs->root;
43+
44+
proc->fdt = std::make_shared<vfs::fdtable>();
45+
proc->cred = std::make_shared<sched::cred_t>();
46+
47+
proc->cred->ruid = proc->cred->euid = proc->cred->suid = 1000;
48+
proc->cred->rgid = proc->cred->egid = proc->cred->sgid = 1000;
49+
50+
lib::path_view tty_path { "/dev/ttyS0" };
51+
ret = vfs::resolve(std::nullopt, tty_path);
52+
if (!ret.has_value())
53+
lib::panic("could not resolve {}", tty_path);
54+
auto tty = vfs::filedesc::create(ret->target, vfs::o_rdwr, proc->pid);
55+
if (!tty || !tty->file)
56+
lib::panic("could not create {} filedesc", tty_path);
57+
if (!tty->file->open(0))
58+
lib::panic("could not open {}", tty_path);
59+
60+
proc->fdt->alloc(tty, 0, false);
61+
proc->fdt->dup(0, 1, false, false);
62+
proc->fdt->dup(0, 2, false, false);
63+
64+
thread = format->load({
65+
.pathname = path.data(),
66+
.file = file,
67+
.interp = { },
68+
.argv = { path.basename().data() },
69+
.envp = {
70+
"TERM=linux",
71+
"USER=ilobilix",
72+
"HOME=/home/ilobilix",
73+
"PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin"
74+
}
75+
}, proc);
76+
77+
if (!thread)
78+
lib::panic("could not create a thread for {}", path);
79+
}
7380

7481
lib::log::wait_for_logs();
75-
// sched::enqueue_new(thread);
82+
sched::enqueue_new(thread);
7683
}
7784

7885
extern "C" [[noreturn]] void kmain()

kernel/source/system/proc/scheduler.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace sched
3939
>, lib::spinlock_irq
4040
> sleep_list;
4141

42-
std::atomic<pid_t> next_id = 1;
42+
std::atomic<pid_t> next_id = 2;
4343
pid_t alloc_id()
4444
{
4545
const auto ret = next_id.fetch_add(1, std::memory_order_relaxed);
@@ -317,6 +317,47 @@ namespace sched
317317
}
318318
}
319319

320+
process_t *create_process(process_t *parent)
321+
{
322+
auto proc = new process_t { };
323+
324+
if (parent == nullptr)
325+
{
326+
proc->pid = 1;
327+
proc->parent = proc;
328+
329+
proc->session = new session_t { };
330+
proc->session->sid = proc->pid;
331+
proc->session->leader = proc;
332+
333+
proc->group = new group_t { };
334+
proc->group->pgid = proc->pid;
335+
proc->group->session = proc->session;
336+
proc->group->leader = proc;
337+
338+
proc->session->foreground_pg = proc->group;
339+
340+
(*proc->group->members.lock())[proc->pid] = proc;
341+
(*proc->session->members.lock())[proc->group->pgid] = proc->group;
342+
}
343+
else
344+
{
345+
proc->pid = alloc_id();
346+
proc->parent = parent;
347+
348+
proc->session = proc->parent->session;
349+
proc->group = proc->parent->group;
350+
351+
(*proc->group->members.lock())[proc->pid] = proc;
352+
}
353+
354+
lib::bug_on(get_process(proc->pid));
355+
(*processes.lock())[proc->pid] = proc;
356+
357+
// the caller will set up the rest of the fields
358+
return proc;
359+
}
360+
320361
thread_t *create_kthread(std::uintptr_t ip, std::uintptr_t arg, nice_t nice)
321362
{
322363
auto proc = get_process(0);
@@ -356,7 +397,11 @@ namespace sched
356397
auto thread = new thread_t { };
357398
thread->self = thread;
358399

359-
thread->tid = alloc_id();
400+
if (auto locked = proc->threads.lock(); locked->empty())
401+
thread->tid = proc->pid;
402+
else
403+
thread->tid = alloc_id();
404+
360405
thread->proc = proc;
361406

362407
thread->nice = nice;

0 commit comments

Comments
 (0)