mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:17:42 +00:00
Kernel: Allocate new main thread stack before committing to exec
If the allocation fails (e.g ENOMEM) we want to simply return an error from sys$execve() and continue executing the current executable. This patch also moves make_userspace_stack_for_main_thread() out of the Thread class since it had nothing in particular to do with Thread.
This commit is contained in:
parent
d55fb7b5e2
commit
89d3b09638
4 changed files with 77 additions and 77 deletions
|
@ -855,78 +855,6 @@ RegisterState& Thread::get_register_dump_from_stack()
|
|||
return *(RegisterState*)(kernel_stack_top() - sizeof(RegisterState));
|
||||
}
|
||||
|
||||
KResultOr<u32> Thread::make_userspace_stack_for_main_thread(Vector<String> arguments, Vector<String> environment, Vector<ELF::AuxiliaryValue> auxiliary_values)
|
||||
{
|
||||
auto* region = m_process->allocate_region(VirtualAddress(), default_userspace_stack_size, "Stack (Main thread)", PROT_READ | PROT_WRITE, false);
|
||||
if (!region)
|
||||
return KResult(-ENOMEM);
|
||||
region->set_stack(true);
|
||||
|
||||
FlatPtr new_esp = region->vaddr().offset(default_userspace_stack_size).get();
|
||||
|
||||
auto push_on_new_stack = [&new_esp](u32 value) {
|
||||
new_esp -= 4;
|
||||
Userspace<u32*> stack_ptr = new_esp;
|
||||
return copy_to_user(stack_ptr, &value);
|
||||
};
|
||||
|
||||
auto push_aux_value_on_new_stack = [&new_esp](auxv_t value) {
|
||||
new_esp -= sizeof(auxv_t);
|
||||
Userspace<auxv_t*> stack_ptr = new_esp;
|
||||
return copy_to_user(stack_ptr, &value);
|
||||
};
|
||||
|
||||
auto push_string_on_new_stack = [&new_esp](const String& string) {
|
||||
new_esp -= round_up_to_power_of_two(string.length() + 1, 4);
|
||||
Userspace<u32*> stack_ptr = new_esp;
|
||||
return copy_to_user(stack_ptr, string.characters(), string.length() + 1);
|
||||
};
|
||||
|
||||
Vector<FlatPtr> argv_entries;
|
||||
for (auto& argument : arguments) {
|
||||
push_string_on_new_stack(argument);
|
||||
argv_entries.append(new_esp);
|
||||
}
|
||||
|
||||
Vector<FlatPtr> env_entries;
|
||||
for (auto& variable : environment) {
|
||||
push_string_on_new_stack(variable);
|
||||
env_entries.append(new_esp);
|
||||
}
|
||||
|
||||
for (auto& value : auxiliary_values) {
|
||||
if (!value.optional_string.is_empty()) {
|
||||
push_string_on_new_stack(value.optional_string);
|
||||
value.auxv.a_un.a_ptr = (void*)new_esp;
|
||||
}
|
||||
}
|
||||
|
||||
for (ssize_t i = auxiliary_values.size() - 1; i >= 0; --i) {
|
||||
auto& value = auxiliary_values[i];
|
||||
push_aux_value_on_new_stack(value.auxv);
|
||||
}
|
||||
|
||||
push_on_new_stack(0);
|
||||
for (ssize_t i = env_entries.size() - 1; i >= 0; --i)
|
||||
push_on_new_stack(env_entries[i]);
|
||||
FlatPtr envp = new_esp;
|
||||
|
||||
push_on_new_stack(0);
|
||||
for (ssize_t i = argv_entries.size() - 1; i >= 0; --i)
|
||||
push_on_new_stack(argv_entries[i]);
|
||||
FlatPtr argv = new_esp;
|
||||
|
||||
// NOTE: The stack needs to be 16-byte aligned.
|
||||
new_esp -= new_esp % 16;
|
||||
|
||||
push_on_new_stack((FlatPtr)envp);
|
||||
push_on_new_stack((FlatPtr)argv);
|
||||
push_on_new_stack((FlatPtr)argv_entries.size());
|
||||
push_on_new_stack(0);
|
||||
|
||||
return new_esp;
|
||||
}
|
||||
|
||||
RefPtr<Thread> Thread::clone(Process& process)
|
||||
{
|
||||
auto clone = adopt(*new Thread(process));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue