Added HandleManager, added mapping to real directories.

This commit is contained in:
Cody Brocious 2016-05-15 09:02:38 -06:00
parent bca8d898b0
commit ae9a1e3de3
15 changed files with 154 additions and 162 deletions

View file

@ -3,14 +3,16 @@
Box *box;
Box::Box() {
box = this;
uint8_t *mem, *kmem;
bailout(!(mem = (uint8_t *) valloc(RAM_SIZE)));
bailout(!(kmem = (uint8_t *) valloc(KRAM_SIZE)));
cpu = new Cpu(mem, kmem);
hypercall = new Hypercall();
hm = new HandleManager();
pm = new PageManager();
pm->add_region(0, 64 * 1024 * 1024);

View file

@ -8,6 +8,7 @@ public:
Cpu *cpu;
Hypercall *hypercall;
HandleManager *hm;
PageManager *pm;
ThreadManager *tm;
IOManager *io;

11
HandleManager.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "Zookeeper.hpp"
uint32_t HandleManager::add_handle(shared_ptr<Handle> handle) {
handle->handle = ++handle_id;
handles[handle_id] = handle;
return handle_id;
}
shared_ptr<Handle> HandleManager::get_handle(uint32_t handle_id) {
return handles[handle_id];
}

26
HandleManager.hpp Normal file
View file

@ -0,0 +1,26 @@
#pragma once
#include "Zookeeper.hpp"
class Handle {
public:
virtual void close() = 0;
uint32_t handle;
};
class HandleManager {
public:
uint32_t add_handle(shared_ptr<Handle> handle);
shared_ptr<Handle> get_handle(uint32_t handle_id);
template<typename T>
uint32_t add_handle(shared_ptr<T> handle) {
return add_handle(static_pointer_cast<Handle>(handle));
}
template<typename T>
shared_ptr<T> get_handle(uint32_t handle_id) {
return dynamic_pointer_cast<T>(get_handle(handle_id));
}
map<uint32_t, shared_ptr<Handle>> handles;
uint32_t handle_id;
};

View file

@ -50,7 +50,7 @@ uint32_t Hypercall::query_eeprom(uint32_t index) {
uint32_t Hypercall::io_open(uint32_t dir_handle, uint32_t fn) {
auto fnstr = read_string(fn);
if(dir_handle != 0) {
auto dirhnd = box->io->get_handle(dir_handle);
auto dirhnd = box->hm->get_handle<DirHandle>(dir_handle);
assert(dirhnd->type == IOType::IO_DIRECTORY);
fnstr = dirhnd->path + "\\" + fnstr;
}

View file

@ -1,46 +1,5 @@
#include "Zookeeper.hpp"
IOManager::IOManager() {
root = make_shared<Directory>();
auto dirs = {
"/Device",
"/Device/CdRom0",
"/Device/Harddisk0",
"/Device/Harddisk0/partition0",
"/Device/Harddisk0/partition1",
"/Device/Harddisk0/partition1/TDATA",
"/Device/Harddisk0/partition1/UDATA",
};
for(auto dir : dirs)
create_directory(dir);
create_link("D:", "/Device/CdRom0");
create_link("T:", "/Device/Harddisk0/partition1/TDATA");
create_link("U:", "/Device/Harddisk0/partition1/UDATA");
}
shared_ptr<IOHandle> IOManager::open(string path) {
auto type = lookup_type(path);
switch(type) {
case IO_DIRECTORY:
return lookup_directory(path)->open();
case IO_FILE:
return lookup_file(path)->open();
case IO_UNKNOWN:
cout << "Attempted to open unknown file/directory: " << path << endl;
return NULL;
}
}
uint32_t IOManager::create_handle() {
assert(handle_id < 0xFFFFFFFF);
return ++handle_id;
}
shared_ptr<IOHandle> IOManager::get_handle(uint32_t handle_id) {
return handles[handle_id];
}
list<string> parse_path(string path) {
list<string> vec;
to_lower(path);
@ -52,19 +11,39 @@ list<string> parse_path(string path) {
return vec;
}
IOType IOManager::lookup_type(string path) {
auto p = parse_path(path);
auto fn = p.back();
p.pop_back();
auto dir = lookup_directory(p);
if(dir == NULL)
return IOType::IO_UNKNOWN;
IOManager::IOManager() {
root = make_shared<Directory>();
auto dirs = {
"/Device",
"/Device/CdRom0",
"/Device/Harddisk0",
"/Device/Harddisk0/partition0",
"/Device/Harddisk0/partition1",
};
for(auto dir : dirs)
create_directory(dir);
if(dir->subdirectories.find(fn) != dir->subdirectories.end())
return IOType::IO_DIRECTORY;
else if(dir->files.find(fn) != dir->files.end())
return IOType::IO_FILE;
return IOType::IO_UNKNOWN;
create_map("/Device/Harddisk0/partition1/TDATA", "fs/tdata");
create_map("/Device/Harddisk0/partition1/UDATA", "fs/udata");
create_link("D:", "/Device/CdRom0");
create_link("T:", "/Device/Harddisk0/partition1/TDATA");
create_link("U:", "/Device/Harddisk0/partition1/UDATA");
open("/Device");
open("T:/foo");
}
shared_ptr<IOHandle> IOManager::open(string pathstr) {
auto path = parse_path(pathstr);
auto dir = lookup_directory(pathstr);
if(dir != NULL)
return dir->open();
auto mapped = lookup_map(pathstr);
auto file = static_pointer_cast<IOHandle>(make_shared<FileHandle>(pathstr, mapped));
box->hm->add_handle(file);
return file;
}
shared_ptr<Directory> IOManager::lookup_directory(string path) {
@ -76,7 +55,7 @@ shared_ptr<Directory> IOManager::lookup_directory(list<string> path) {
for(auto e : path) {
if(dir->subdirectories.find(e) == dir->subdirectories.end()) {
cout << "Could not find " << e << " in path " << join(path, "\\") << endl;
bailout(true);
return NULL;
}
dir = dir->subdirectories[e];
@ -84,19 +63,29 @@ shared_ptr<Directory> IOManager::lookup_directory(list<string> path) {
return dir;
}
shared_ptr<File> IOManager::lookup_file(string path) {
auto p = parse_path(path);
auto fn = p.back();
p.pop_back();
auto dir = lookup_directory(p);
if(dir == NULL)
return NULL;
if(dir->files.find(fn) == dir->files.end()) {
cout << "Could not find file " << path << endl;
bailout(true);
string IOManager::lookup_map(string pathstr) {
auto path = parse_path(pathstr);
auto dir = root;
auto i = 0;
for(auto e : path) {
++i;
if(dir->subdirectories.find(e) != dir->subdirectories.end())
dir = dir->subdirectories[e];
else if(dir->dirmaps.find(e) != dir->dirmaps.end()) {
auto base = dir->dirmaps[e];
if(i == path.size())
return base;
else {
auto bi = next(path.begin(), i);
list<string> sub(bi, path.end());
return base + "/" + join(sub, "/");
}
} else {
cout << "Could not find " << e << " in path " << pathstr << endl;
return NULL;
}
}
return dir->files[fn];
return NULL;
}
shared_ptr<Directory> IOManager::create_directory(string path) {
@ -110,61 +99,41 @@ shared_ptr<Directory> IOManager::create_directory(string path) {
return ndir;
}
shared_ptr<File> IOManager::create_file(string path, function<shared_ptr<IOHandle>()> init) {
auto p = parse_path(path);
auto fn = p.back();
p.pop_back();
auto dir = lookup_directory(p);
auto nf = make_shared<File>(init);
dir->files[fn] = nf;
return nf;
}
void IOManager::create_link(string from, string to) {
auto p = parse_path(from);
auto fn = p.back();
p.pop_back();
auto dir = lookup_directory(p);
switch(lookup_type(to)) {
case IO_DIRECTORY: {
auto target = lookup_directory(to);
dir->subdirectories[fn] = target;
break;
}
case IO_FILE: {
auto target = lookup_file(to);
dir->files[fn] = target;
break;
}
case IO_UNKNOWN:
bailout("Could not find link target");
auto target = lookup_directory(to);
if(target != NULL) {
dir->subdirectories[fn] = target;
return;
}
auto target_map = lookup_map(to);
dir->dirmaps[fn] = target_map;
}
IOHandle::IOHandle() {
handle = box->io->create_handle();
}
File::File(function<shared_ptr<IOHandle>()> _init) {
init = _init;
}
shared_ptr<IOHandle> File::open() {
auto hnd = init();
hnd->type = IOType::IO_FILE;
hnd->path = path;
return hnd;
void IOManager::create_map(string from, string to) {
auto p = parse_path(from);
auto fn = p.back();
p.pop_back();
auto dir = lookup_directory(p);
dir->dirmaps[fn] = to;
}
shared_ptr<IOHandle> Directory::open() {
auto hnd = static_pointer_cast<IOHandle>(make_shared<DirHandle>());
hnd->type = IOType::IO_DIRECTORY;
hnd->path = path;
auto hnd = static_pointer_cast<IOHandle>(make_shared<DirHandle>(path));
box->hm->add_handle(hnd);
return hnd;
}
IOHandle::IOHandle(IOType type, string path) : type(type), path(path) {
}
FileHandle::FileHandle(string path, string mapped_path) : IOHandle(IOType::IO_FILE, path), mapped_path(mapped_path) {
cout << "Creating file handle for virtual path " << path << " to real path " << mapped_path << endl;
}
void FileHandle::read() {
}
void FileHandle::write() {
@ -172,9 +141,7 @@ void FileHandle::write() {
void FileHandle::close() {
}
void DirHandle::read() {
}
void DirHandle::write() {
DirHandle::DirHandle(string path) : IOHandle(IOType::IO_DIRECTORY, path) {
}
void DirHandle::close() {
}

View file

@ -7,47 +7,35 @@ enum IOType {
IO_DIRECTORY
};
class IOHandle {
class IOHandle : public Handle {
public:
IOHandle();
virtual void read() = 0;
virtual void write() = 0;
virtual void close() = 0;
uint32_t handle;
IOHandle(IOType type, string path);
IOType type;
string path;
};
class FileHandle : public IOHandle {
public:
FileHandle(string path, string mapped_path);
void read();
void write();
void close();
string mapped_path;
};
class DirHandle : public IOHandle {
public:
void read();
void write();
DirHandle(string path);
void close();
};
class File {
public:
File(function<shared_ptr<IOHandle>()> _init);
shared_ptr<IOHandle> open();
string path;
function<shared_ptr<IOHandle>()> init;
};
class Directory {
public:
shared_ptr<IOHandle> open();
map<string, shared_ptr<Directory>> subdirectories;
map<string, shared_ptr<File>> files;
map<string, string> dirmaps;
string path;
};
@ -55,19 +43,15 @@ class IOManager {
public:
IOManager();
shared_ptr<IOHandle> open(string fn);
uint32_t create_handle();
shared_ptr<IOHandle> get_handle(uint32_t handle_id);
shared_ptr<IOHandle> open_file(string path);
IOType lookup_type(string path);
shared_ptr<File> lookup_file(string path);
shared_ptr<Directory> lookup_directory(string path);
shared_ptr<Directory> lookup_directory(list<string> path);
string lookup_map(string path);
shared_ptr<Directory> create_directory(string path);
shared_ptr<File> create_file(string path, function<shared_ptr<IOHandle>()>);
void create_link(string from, string to);
void create_map(string from, string to);
map<uint32_t, shared_ptr<IOHandle>> handles;
uint32_t handle_id;
shared_ptr<Directory> root;
};

View file

@ -6,7 +6,7 @@ CC_FLAGS := -std=c++11 -I. -ffreestanding -c -nostdlib -O2 -fno-exceptions -fno-
AS_FLAGS := -c $(TARGET_FLAGS)
LD_FLAGS := $(TARGET_FLAGS) -ffreestanding -nostdlib -Wl,-image_base,0xC0000000
KH_FILES := $(wildcard XboxKernel/*.hpp)
KH_FILES := $(filter-out XboxKernel/KernelThunk.hpp, $(wildcard XboxKernel/*.hpp))
# This needs to be explicit because KernelThunk doesn't exist before generation.
ifeq ($(wildcard XboxKernel/KernelThunk.cpp),)

View file

@ -15,6 +15,25 @@ NTSTATUS NTAPI kernel_NtOpenFile(
return STATUS_SUCCESS;
}
NTSTATUS NTAPI kernel_NtCreateFile(
OUT PHANDLE FileHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
OUT PIO_STATUS_BLOCK IoStatusBlock,
IN PLARGE_INTEGER AllocationSize,
IN ULONG FileAttributes,
IN ULONG ShareAccess,
IN ULONG CreateDisposition,
IN ULONG CreateOptions
) {
log("NtCreateFile('%s')", ObjectAttributes->ObjectName->Buffer);
*FileHandle = io_open(ObjectAttributes->RootDirectory, (char *) ObjectAttributes->ObjectName->Buffer);
if(*FileHandle == 0) {
return STATUS_OBJECT_NAME_NOT_FOUND;
}
return STATUS_SUCCESS;
}
NTSTATUS NTAPI kernel_NtClose(HANDLE handle) {
log("NtClose %08x", handle);
return STATUS_SUCCESS;
@ -54,22 +73,3 @@ NTSTATUS NTAPI kernel_IoCreateSymbolicLink(
log("IoCreateSymbolicLink('%s', '%s')", SymbolicLinkName->Buffer, DeviceName->Buffer);
return STATUS_SUCCESS;
}
NTSTATUS NTAPI kernel_NtCreateFile(
OUT PHANDLE FileHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
OUT PIO_STATUS_BLOCK IoStatusBlock,
IN PLARGE_INTEGER AllocationSize,
IN ULONG FileAttributes,
IN ULONG ShareAccess,
IN ULONG CreateDisposition,
IN ULONG CreateOptions
) {
log("NtCreateFile('%s')", ObjectAttributes->ObjectName->Buffer);
*FileHandle = io_open(ObjectAttributes->RootDirectory, (char *) ObjectAttributes->ObjectName->Buffer);
if(*FileHandle == 0) {
return STATUS_OBJECT_NAME_NOT_FOUND;
}
return STATUS_SUCCESS;
}

View file

@ -5,7 +5,7 @@ void NTAPI kernel_RtlInitAnsiString(
char *SourceString
) {
auto len = DestinationString->Length = DestinationString->MaximumLength = strlen(SourceString);
DestinationString->Buffer = new char[len + 1];
DestinationString->Buffer = new CHAR[len + 1];
memcpy(DestinationString->Buffer, SourceString, len + 1);
}
@ -15,7 +15,7 @@ bool NTAPI kernel_RtlEqualString(
bool CaseInsensitive
) {
if(CaseInsensitive)
return stricmp(String1->Buffer, String2->Buffer) == 0;
return stricmp((char *) String1->Buffer, (char *) String2->Buffer) == 0;
else
return strcmp(String1->Buffer, String2->Buffer) == 0;
return strcmp((char *) String1->Buffer, (char *) String2->Buffer) == 0;
}

View file

@ -200,7 +200,7 @@ typedef uint32_t ACCESS_MASK;
typedef struct _STRING {
USHORT Length;
USHORT MaximumLength;
char *Buffer;
PCHAR Buffer;
} STRING, ANSI_STRING, *PSTRING, *PANSI_STRING;
typedef struct _OBJECT_ATTRIBUTES {

View file

@ -26,6 +26,7 @@ using namespace boost::algorithm;
#include "xbetypes.hpp"
#include "Xbe.hpp"
#include "Cpu.hpp"
#include "HandleManager.hpp"
#include "PageManager.hpp"
#include "ThreadManager.hpp"
#include "IOManager.hpp"

0
fs/tdata/.keep Normal file
View file

0
fs/udata/.keep Normal file
View file

View file

@ -44,7 +44,7 @@ uint32_t load_kernel(Cpu *cpu) {
}
int main(int argc, char **argv) {
box = new Box;
new Box;
uint32_t entry = load_kernel(box->cpu);
auto xbe = new Xbe((char *) "test1.xbe");