summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/src/CodeMgr.cc
blob: 53576b2cecbb93b26b78438d7391202f1b06149c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* -------------------------------------------

	Copyright (C) 2024, Theater Quality Corp, all rights reserved.

------------------------------------------- */

#include <KernelKit/CodeMgr.h>
#include <NewKit/Utils.h>
#include <KernelKit/UserProcessScheduler.h>

namespace Kernel
{
	/***********************************************************************************/
	/// @brief Executes a new process from a function. Kernel code only.
	/// @note This sets up a new stack, anything on the main function that calls the Kernel will not be accessible.
	/// @param main the start of the process.
	/// @return if the process was started or not.
	/***********************************************************************************/

	ProcessID rtl_create_process(rtl_main_kind main, const Char* process_name) noexcept
	{
		if (!process_name ||
			*process_name == 0)
			return kProcessInvalidID;

		UserProcess* process_hdr = new UserProcess();

		process_hdr->Image.fCode = reinterpret_cast<VoidPtr>(main);
		process_hdr->Kind		 = UserProcess::kExectuableKind;
		process_hdr->StackSize	 = kib_cast(8);

		rt_set_memory(process_hdr->Name, 0, kProcessNameLen);
		rt_copy_memory((VoidPtr)process_name, process_hdr->Name, rt_string_len(process_name));

		ProcessID id = UserProcessScheduler::The().Spawn(process_hdr);

		if (id == kProcessInvalidID)
			delete process_hdr;

		return id;
	}
} // namespace Kernel