2014-12-17 08:38:14 +03:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
2014-11-19 11:49:13 +03:00
|
|
|
// Refer to the license.txt file included.
|
2014-05-10 06:11:18 +04:00
|
|
|
|
2014-12-04 03:55:45 +03:00
|
|
|
#include <algorithm>
|
|
|
|
|
2014-05-10 06:11:18 +04:00
|
|
|
#include "common/common.h"
|
|
|
|
|
2014-05-14 05:57:12 +04:00
|
|
|
#include "core/core.h"
|
2014-05-10 06:11:18 +04:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/thread.h"
|
|
|
|
|
2014-05-21 02:13:25 +04:00
|
|
|
namespace Kernel {
|
2014-05-10 06:11:18 +04:00
|
|
|
|
2014-06-02 05:42:50 +04:00
|
|
|
Handle g_main_thread = 0;
|
2014-12-14 02:16:13 +03:00
|
|
|
HandleTable g_handle_table;
|
2014-12-16 08:33:41 +03:00
|
|
|
u64 g_program_id = 0;
|
2014-05-10 06:11:18 +04:00
|
|
|
|
2014-12-14 02:16:13 +03:00
|
|
|
HandleTable::HandleTable() {
|
2014-05-10 06:11:18 +04:00
|
|
|
next_id = INITIAL_NEXT_ID;
|
|
|
|
}
|
|
|
|
|
2014-12-14 02:16:13 +03:00
|
|
|
Handle HandleTable::Create(Object* obj, int range_bottom, int range_top) {
|
2014-05-10 06:11:18 +04:00
|
|
|
if (range_top > MAX_COUNT) {
|
|
|
|
range_top = MAX_COUNT;
|
|
|
|
}
|
|
|
|
if (next_id >= range_bottom && next_id < range_top) {
|
|
|
|
range_bottom = next_id++;
|
|
|
|
}
|
|
|
|
for (int i = range_bottom; i < range_top; i++) {
|
|
|
|
if (!occupied[i]) {
|
|
|
|
occupied[i] = true;
|
|
|
|
pool[i] = obj;
|
2014-05-16 02:26:28 +04:00
|
|
|
pool[i]->handle = i + HANDLE_OFFSET;
|
2014-05-10 06:11:18 +04:00
|
|
|
return i + HANDLE_OFFSET;
|
|
|
|
}
|
|
|
|
}
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_ERROR(Kernel, "Unable to allocate kernel object, too many objects slots in use.");
|
2014-05-10 06:11:18 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-14 02:16:13 +03:00
|
|
|
bool HandleTable::IsValid(Handle handle) const {
|
2014-05-10 06:11:18 +04:00
|
|
|
int index = handle - HANDLE_OFFSET;
|
|
|
|
if (index < 0)
|
|
|
|
return false;
|
|
|
|
if (index >= MAX_COUNT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return occupied[index];
|
|
|
|
}
|
|
|
|
|
2014-12-14 02:16:13 +03:00
|
|
|
void HandleTable::Clear() {
|
2014-05-21 02:13:25 +04:00
|
|
|
for (int i = 0; i < MAX_COUNT; i++) {
|
2014-05-10 06:11:18 +04:00
|
|
|
//brutally clear everything, no validation
|
|
|
|
if (occupied[i])
|
|
|
|
delete pool[i];
|
|
|
|
occupied[i] = false;
|
|
|
|
}
|
2014-08-19 08:20:56 +04:00
|
|
|
pool.fill(nullptr);
|
2014-05-10 06:11:18 +04:00
|
|
|
next_id = INITIAL_NEXT_ID;
|
|
|
|
}
|
|
|
|
|
2014-12-14 02:16:13 +03:00
|
|
|
Object* &HandleTable::operator [](Handle handle)
|
2014-05-10 06:11:18 +04:00
|
|
|
{
|
2014-12-06 04:53:49 +03:00
|
|
|
_dbg_assert_msg_(Kernel, IsValid(handle), "GRABBING UNALLOCED KERNEL OBJ");
|
2014-05-10 06:11:18 +04:00
|
|
|
return pool[handle - HANDLE_OFFSET];
|
|
|
|
}
|
|
|
|
|
2014-12-14 02:16:13 +03:00
|
|
|
void HandleTable::List() {
|
2014-05-10 06:11:18 +04:00
|
|
|
for (int i = 0; i < MAX_COUNT; i++) {
|
|
|
|
if (occupied[i]) {
|
|
|
|
if (pool[i]) {
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_DEBUG(Kernel, "KO %i: %s \"%s\"", i + HANDLE_OFFSET, pool[i]->GetTypeName().c_str(),
|
2014-08-18 07:03:22 +04:00
|
|
|
pool[i]->GetName().c_str());
|
2014-05-10 06:11:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-14 02:16:13 +03:00
|
|
|
int HandleTable::GetCount() const {
|
2014-12-04 03:55:45 +03:00
|
|
|
return std::count(occupied.begin(), occupied.end(), true);
|
2014-05-10 06:11:18 +04:00
|
|
|
}
|
|
|
|
|
2014-12-14 02:16:13 +03:00
|
|
|
Object* HandleTable::CreateByIDType(int type) {
|
2014-12-06 04:53:49 +03:00
|
|
|
LOG_ERROR(Kernel, "Unimplemented: %d.", type);
|
2014-09-14 06:48:30 +04:00
|
|
|
return nullptr;
|
2014-05-10 06:11:18 +04:00
|
|
|
}
|
2014-05-14 05:57:12 +04:00
|
|
|
|
2014-06-11 06:43:50 +04:00
|
|
|
/// Initialize the kernel
|
2014-05-21 02:13:25 +04:00
|
|
|
void Init() {
|
2014-05-21 03:37:46 +04:00
|
|
|
Kernel::ThreadingInit();
|
2014-05-21 02:13:25 +04:00
|
|
|
}
|
|
|
|
|
2014-06-11 06:43:50 +04:00
|
|
|
/// Shutdown the kernel
|
2014-05-21 02:13:25 +04:00
|
|
|
void Shutdown() {
|
2014-05-21 03:37:46 +04:00
|
|
|
Kernel::ThreadingShutdown();
|
2014-06-11 06:43:50 +04:00
|
|
|
|
2014-12-14 02:16:13 +03:00
|
|
|
g_handle_table.Clear(); // Free all kernel objects
|
2014-05-21 02:13:25 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 03:06:12 +04:00
|
|
|
/**
|
|
|
|
* Loads executable stored at specified address
|
|
|
|
* @entry_point Entry point in memory of loaded executable
|
|
|
|
* @return True on success, otherwise false
|
|
|
|
*/
|
|
|
|
bool LoadExec(u32 entry_point) {
|
2014-05-14 05:57:12 +04:00
|
|
|
Core::g_app_core->SetPC(entry_point);
|
|
|
|
|
2014-05-15 04:49:27 +04:00
|
|
|
// 0x30 is the typical main thread priority I've seen used so far
|
2014-06-05 08:20:58 +04:00
|
|
|
g_main_thread = Kernel::SetupMainThread(0x30);
|
2014-05-14 05:57:12 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-23 03:06:12 +04:00
|
|
|
|
|
|
|
} // namespace
|