Cleanup ThreadState and XThread

This commit is contained in:
Dr. Chat
2015-11-27 23:07:06 -06:00
committed by Ben Vanik
parent 41d5b41523
commit 666f5543a8
6 changed files with 133 additions and 150 deletions

View File

@@ -26,16 +26,10 @@ namespace cpu {
thread_local ThreadState* thread_state_ = nullptr;
ThreadState::ThreadState(Processor* processor, uint32_t thread_id,
ThreadStackType stack_type, uint32_t stack_address,
uint32_t stack_size, uint32_t pcr_address)
uint32_t stack_base, uint32_t pcr_address)
: processor_(processor),
memory_(processor->memory()),
thread_id_(thread_id),
stack_type_(stack_type),
name_(""),
backend_data_(0),
stack_size_(stack_size),
pcr_address_(pcr_address) {
thread_id_(thread_id) {
if (thread_id_ == UINT_MAX) {
// System thread. Assign the system thread ID with a high bit
// set so people know what's up.
@@ -44,44 +38,6 @@ ThreadState::ThreadState(Processor* processor, uint32_t thread_id,
}
backend_data_ = processor->backend()->AllocThreadData();
if (!stack_address) {
// We must always allocate 64K as a guard region before stacks, as we can
// only Protect() on system page granularity.
auto heap = memory()->LookupHeap(0x40000000);
stack_size = (stack_size + 0xFFF) & 0xFFFFF000;
uint32_t stack_alignment = (stack_size & 0xF000) ? 0x1000 : 0x10000;
uint32_t stack_padding = heap->page_size();
uint32_t actual_stack_size = stack_padding + stack_size;
bool top_down = false;
switch (stack_type) {
case ThreadStackType::kKernelStack:
top_down = true;
break;
case ThreadStackType::kUserStack:
top_down = false;
break;
default:
assert_unhandled_case(stack_type);
break;
}
heap->AllocRange(0x40000000, 0x7FFFFFFF, actual_stack_size, stack_alignment,
kMemoryAllocationReserve | kMemoryAllocationCommit,
kMemoryProtectRead | kMemoryProtectWrite, top_down,
&stack_address_);
assert_true(!(stack_address_ & 0xFFF)); // just to be safe
stack_allocated_ = true;
stack_base_ = stack_address_ + actual_stack_size;
stack_limit_ = stack_address_ + stack_padding;
memory()->Fill(stack_address_, actual_stack_size, 0xBE);
heap->Protect(stack_address_, stack_padding, kMemoryProtectNoAccess);
} else {
stack_address_ = stack_address;
stack_allocated_ = false;
stack_base_ = stack_address_ + stack_size;
stack_limit_ = stack_address_;
}
assert_not_zero(stack_address_);
// Allocate with 64b alignment.
context_ = memory::AlignedAlloc<ppc::PPCContext>(64);
assert_true(((uint64_t)context_ & 0x3F) == 0);
@@ -96,8 +52,8 @@ ThreadState::ThreadState(Processor* processor, uint32_t thread_id,
context_->thread_id = thread_id_;
// Set initial registers.
context_->r[1] = stack_base_;
context_->r[13] = pcr_address_;
context_->r[1] = stack_base;
context_->r[13] = pcr_address;
}
ThreadState::~ThreadState() {
@@ -109,9 +65,6 @@ ThreadState::~ThreadState() {
}
memory::AlignedFree(context_);
if (stack_allocated_) {
memory()->LookupHeap(stack_address_)->Decommit(stack_address_, stack_size_);
}
}
void ThreadState::Bind(ThreadState* thread_state) {