Added memset/memcpy. Finished TIB/TLS initialization.

This commit is contained in:
Cody Brocious 2016-05-09 15:08:58 -06:00
parent 179251fd94
commit b9f09fe6c5
9 changed files with 52 additions and 13 deletions

15
NightBeliever/C.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "NightBeliever.hpp"
void *memcpy(void *dest, const void *src, size_t n) {
auto a = (uint8_t *) dest, b = (uint8_t *) src;
while(n--)
*(a++) = *(b++);
return dest;
}
void *memset(void *ptr, int value, size_t num) {
auto chrs = (uint8_t *) ptr;
while(num--)
chrs[num] = (uint8_t) value;
return ptr;
}

6
NightBeliever/C.hpp Normal file
View file

@ -0,0 +1,6 @@
#include "NightBeliever.hpp"
typedef uint32_t size_t;
void *memcpy(void *dest, const void *src, size_t n);
void *memset(void * ptr, int value, size_t num);

View file

@ -4,6 +4,7 @@
#define NTAPI __attribute__((stdcall))
#include <stdint.h>
#include "C.hpp"
#include "mini-printf.hpp"
#include "../xbetypes.hpp"
#include "TIB.hpp"

View file

@ -1,5 +1,7 @@
#include "NightBeliever.hpp"
XbeTLS_t *global_tls;
void gdt_encode(uint8_t *gdt, int entry, uint32_t base, uint32_t limit, uint8_t type) {
gdt += 8 * entry;
if(limit > 65536) {
@ -21,7 +23,7 @@ void gdt_encode(uint8_t *gdt, int entry, uint32_t base, uint32_t limit, uint8_t
}
void init_tib(uint32_t tid) {
auto gdt = (uint8_t *) (96 * 1024 * 1024);
auto gdt = (uint8_t *) (96 * 1024 * 1024); // XXX: Should pass GDT and other things in a struct at startup.
auto entry = -1;
for(auto i = 3 * 8; i < 8192 * 8; i += 8) {
if((gdt[i + 6] & 0x80) == 0) {
@ -35,13 +37,25 @@ void init_tib(uint32_t tid) {
halt();
}
auto tls = new uint8_t[0x10000];
auto copy = global_tls->data_end - global_tls->data_start;
// Weird padding dance
auto tls = new uint8_t[copy + global_tls->zero_fill + 15] + 4;
while((((uint32_t) tls) & 0xF) != 0)
tls += 1;
tls -= 4;
memcpy(tls, (uint8_t *) global_tls->data_start, copy);
memset(tls + copy, 0, global_tls->zero_fill);
auto index = (uint32_t *) global_tls->index;
*index = 0;
auto ethread = new ETHREAD;
ethread->Tcb.TlsData = tls;
ethread->UniqueThread = tid;
auto tib = new _KPCR;
tib->NtTib.StackBase = tls;
tib->NtTib.Self = &tib->NtTib;
tib->SelfPcr = tib;
tib->PrcbData.CurrentThread = (KTHREAD *) ethread;

View file

@ -1,4 +1,6 @@
#pragma once
#include "NightBeliever.hpp"
extern XbeTLS_t *global_tls;
void init_tib(uint32_t tid);

View file

@ -4,8 +4,6 @@ typedef void(*xbe_ep_t)();
void entrypoint() {
log("NightBeliever initializing...");
init_tib(0);
log("Idle.");
auto xbe = get_xbebase();
auto thunk = (uint32_t *) xbe->thunk;
@ -13,6 +11,11 @@ void entrypoint() {
*thunk = thunk_lookup(*thunk);
++thunk;
}
global_tls = (XbeTLS_t *) xbe->tls;
init_tib(0);
log("Calling entrypoint.");
auto ep = (xbe_ep_t) xbe->oep;
ep();

View file

@ -7,12 +7,6 @@
// skip the define.
#ifndef _ALLOC_SKIP_DEFINE
#ifndef _HAVE_SIZE_T
#define _HAVE_SIZE_T
typedef unsigned int size_t;
#endif
#ifndef NULL
#define NULL 0
#endif

View file

@ -47,8 +47,7 @@ typedef struct _ETHREAD
struct _KTHREAD Tcb;
UCHAR UnknownA[0x1C]; // 0x110
DWORD UniqueThread; // 0x12C
}
ETHREAD, *PETHREAD;
} ETHREAD, *PETHREAD;
typedef struct _KPRCB
{

View file

@ -17,4 +17,9 @@ typedef struct XbeSection {
uint32_t flags, vaddr, vsize, raddr, rsize;
uint32_t nameaddr, nameref, headref, tailref;
uint8_t digest[20];
} XbeSection_t;
} XbeSection_t;
typedef struct XbeTLS {
uint32_t data_start, data_end, index, callback;
uint32_t zero_fill, characteristics;
} XbeTLS_t;