xenia-canary/src/xenia/kernel/kernel_state.h

223 lines
7.2 KiB
C
Raw Normal View History

/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_KERNEL_KERNEL_STATE_H_
#define XENIA_KERNEL_KERNEL_STATE_H_
#include <atomic>
#include <condition_variable>
#include <functional>
#include <list>
2014-08-03 21:38:04 +00:00
#include <memory>
2015-08-08 04:29:03 +00:00
#include <vector>
2014-08-03 21:38:04 +00:00
#include "xenia/base/bit_map.h"
#include "xenia/base/cvar.h"
#include "xenia/base/mutex.h"
#include "xenia/cpu/export_resolver.h"
2015-09-06 20:45:52 +00:00
#include "xenia/kernel/util/native_list.h"
#include "xenia/kernel/util/object_table.h"
2015-09-07 01:07:52 +00:00
#include "xenia/kernel/xam/app_manager.h"
#include "xenia/kernel/xam/content_manager.h"
#include "xenia/kernel/xam/user_profile.h"
2015-02-01 06:49:47 +00:00
#include "xenia/memory.h"
#include "xenia/vfs/virtual_file_system.h"
2015-02-01 06:49:47 +00:00
#include "xenia/xbox.h"
2014-08-17 02:07:21 +00:00
namespace xe {
class ByteStream;
2014-08-17 02:07:21 +00:00
class Emulator;
namespace cpu {
class Processor;
} // namespace cpu
} // namespace xe
namespace xe {
namespace kernel {
2014-08-17 02:07:21 +00:00
class Dispatcher;
class XHostThread;
2015-09-07 01:07:52 +00:00
class KernelModule;
2014-08-17 02:07:21 +00:00
class XModule;
class XNotifyListener;
2014-08-17 02:07:21 +00:00
class XThread;
2015-09-07 01:07:52 +00:00
class UserModule;
// (?), used by KeGetCurrentProcessType
constexpr uint32_t X_PROCTYPE_IDLE = 0;
constexpr uint32_t X_PROCTYPE_USER = 1;
constexpr uint32_t X_PROCTYPE_SYSTEM = 2;
2015-06-05 00:46:00 +00:00
struct ProcessInfoBlock {
xe::be<uint32_t> unk_00;
xe::be<uint32_t> unk_04; // blink
xe::be<uint32_t> unk_08; // flink
xe::be<uint32_t> unk_0C;
xe::be<uint32_t> unk_10;
xe::be<uint32_t> thread_count;
2018-05-27 21:34:35 +00:00
uint8_t unk_18;
uint8_t unk_19;
uint8_t unk_1A;
uint8_t unk_1B;
2015-06-05 00:46:00 +00:00
xe::be<uint32_t> kernel_stack_size;
xe::be<uint32_t> unk_20;
xe::be<uint32_t> tls_data_size;
xe::be<uint32_t> tls_raw_data_size;
xe::be<uint16_t> tls_slot_size;
2018-05-27 21:34:35 +00:00
uint8_t unk_2E;
uint8_t process_type;
2015-06-05 00:46:00 +00:00
xe::be<uint32_t> bitmap[0x20 / 4];
xe::be<uint32_t> unk_50;
xe::be<uint32_t> unk_54; // blink
xe::be<uint32_t> unk_58; // flink
xe::be<uint32_t> unk_5C;
};
struct TerminateNotification {
uint32_t guest_routine;
uint32_t priority;
};
class KernelState {
2014-08-17 02:07:21 +00:00
public:
2015-08-08 04:29:03 +00:00
explicit KernelState(Emulator* emulator);
~KernelState();
static KernelState* shared();
Emulator* emulator() const { return emulator_; }
Memory* memory() const { return memory_; }
cpu::Processor* processor() const { return processor_; }
vfs::VirtualFileSystem* file_system() const { return file_system_; }
uint32_t title_id() const;
2015-09-07 01:07:52 +00:00
xam::AppManager* app_manager() const { return app_manager_.get(); }
xam::ContentManager* content_manager() const {
return content_manager_.get();
}
xam::UserProfile* user_profile() const { return user_profile_.get(); }
2014-08-03 21:38:04 +00:00
// Access must be guarded by the global critical region.
2015-09-06 20:45:52 +00:00
util::ObjectTable* object_table() { return &object_table_; }
2015-06-05 00:46:00 +00:00
uint32_t process_type() const;
void set_process_type(uint32_t value);
uint32_t process_info_block_address() const {
return process_info_block_address_;
}
2014-11-01 18:42:44 +00:00
uint32_t AllocateTLS();
void FreeTLS(uint32_t slot);
void RegisterTitleTerminateNotification(uint32_t routine, uint32_t priority);
void RemoveTitleTerminateNotification(uint32_t routine);
void RegisterModule(XModule* module);
void UnregisterModule(XModule* module);
bool RegisterUserModule(object_ref<UserModule> module);
void UnregisterUserModule(UserModule* module);
bool IsKernelModule(const char* name);
object_ref<XModule> GetModule(const char* name, bool user_only = false);
2015-07-05 22:27:25 +00:00
object_ref<XThread> LaunchModule(object_ref<UserModule> module);
2015-09-07 01:07:52 +00:00
object_ref<UserModule> GetExecutableModule();
void SetExecutableModule(object_ref<UserModule> module);
object_ref<UserModule> LoadUserModule(const char* name,
bool call_entry = true);
2015-07-05 22:27:25 +00:00
2015-09-07 01:07:52 +00:00
object_ref<KernelModule> GetKernelModule(const char* name);
template <typename T>
2015-09-07 01:07:52 +00:00
object_ref<KernelModule> LoadKernelModule() {
auto kernel_module = object_ref<KernelModule>(new T(emulator_, this));
2015-05-25 03:44:27 +00:00
LoadKernelModule(kernel_module);
return kernel_module;
}
2015-07-05 22:27:25 +00:00
template <typename T>
object_ref<T> GetKernelModule(const char* name) {
auto module = GetKernelModule(name);
return object_ref<T>(reinterpret_cast<T*>(module.release()));
}
2015-07-05 20:42:30 +00:00
// Terminates a title: Unloads all modules, and kills all guest threads.
// This DOES NOT RETURN if called from a guest thread!
void TerminateTitle();
2015-07-05 20:42:30 +00:00
2014-01-08 04:54:47 +00:00
void RegisterThread(XThread* thread);
void UnregisterThread(XThread* thread);
void OnThreadExecute(XThread* thread);
void OnThreadExit(XThread* thread);
2015-05-25 03:50:52 +00:00
object_ref<XThread> GetThreadByID(uint32_t thread_id);
2014-01-08 04:54:47 +00:00
void RegisterNotifyListener(XNotifyListener* listener);
void UnregisterNotifyListener(XNotifyListener* listener);
2014-06-23 02:41:26 +00:00
void BroadcastNotification(XNotificationID id, uint32_t data);
2015-09-06 20:45:52 +00:00
util::NativeList* dpc_list() { return &dpc_list_; }
void CompleteOverlapped(uint32_t overlapped_ptr, X_RESULT result);
void CompleteOverlappedEx(uint32_t overlapped_ptr, X_RESULT result,
uint32_t extended_error, uint32_t length);
void CompleteOverlappedImmediate(uint32_t overlapped_ptr, X_RESULT result);
void CompleteOverlappedImmediateEx(uint32_t overlapped_ptr, X_RESULT result,
uint32_t extended_error, uint32_t length);
void CompleteOverlappedDeferred(std::function<void()> completion_callback,
uint32_t overlapped_ptr, X_RESULT result);
void CompleteOverlappedDeferredEx(std::function<void()> completion_callback,
uint32_t overlapped_ptr, X_RESULT result,
uint32_t extended_error, uint32_t length);
2014-08-04 04:26:10 +00:00
bool Save(ByteStream* stream);
bool Restore(ByteStream* stream);
2014-08-17 02:07:21 +00:00
private:
2015-09-07 01:07:52 +00:00
void LoadKernelModule(object_ref<KernelModule> kernel_module);
2014-08-17 02:07:21 +00:00
Emulator* emulator_;
Memory* memory_;
cpu::Processor* processor_;
vfs::VirtualFileSystem* file_system_;
2015-09-07 01:07:52 +00:00
std::unique_ptr<xam::AppManager> app_manager_;
std::unique_ptr<xam::ContentManager> content_manager_;
std::unique_ptr<xam::UserProfile> user_profile_;
2014-08-03 21:38:04 +00:00
xe::global_critical_region global_critical_region_;
// Must be guarded by the global critical region.
2015-09-06 20:45:52 +00:00
util::ObjectTable object_table_;
2014-01-08 04:54:47 +00:00
std::unordered_map<uint32_t, XThread*> threads_by_id_;
std::vector<object_ref<XNotifyListener>> notify_listeners_;
bool has_notified_startup_ = false;
uint32_t process_type_ = X_PROCTYPE_USER;
2015-09-07 01:07:52 +00:00
object_ref<UserModule> executable_module_;
std::vector<object_ref<KernelModule>> kernel_modules_;
std::vector<object_ref<UserModule>> user_modules_;
std::vector<TerminateNotification> terminate_notifications_;
2015-05-03 22:17:22 +00:00
uint32_t process_info_block_address_ = 0;
2015-06-05 00:46:00 +00:00
2015-07-16 06:26:58 +00:00
std::atomic<bool> dispatch_thread_running_;
object_ref<XHostThread> dispatch_thread_;
// Must be guarded by the global critical region.
2015-09-06 20:45:52 +00:00
util::NativeList dpc_list_;
std::condition_variable_any dispatch_cond_;
std::list<std::function<void()>> dispatch_queue_;
BitMap tls_bitmap_;
friend class XObject;
};
} // namespace kernel
} // namespace xe
#endif // XENIA_KERNEL_KERNEL_STATE_H_