Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Pointers casting refactoring - continue
  • Loading branch information
djphoenix committed Nov 2, 2016
1 parent dce03b3 commit 27cdf0f
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 245 deletions.
2 changes: 1 addition & 1 deletion CPPLINT.cfg
@@ -1,2 +1,2 @@
set noparent
filter=-readability/casting,-build/include_what_you_use,-readability/todo
filter=-build/include_what_you_use,-readability/todo
4 changes: 2 additions & 2 deletions include/acpi.hpp
Expand Up @@ -114,14 +114,14 @@ struct ioapic_redir {
class ACPI {
private:
AcpiMadt *madt;
uint32_t* localApicAddr, *ioApicAddr;
char *localApicAddr, *ioApicAddr;
uint8_t ioApicMaxCount;
uint32_t acpiCpuIds[256];
uint8_t acpiCpuCount;
static uint8_t activeCpuCount;
static uint64_t busfreq;
static ACPI *controller;
bool ParseRsdp(char* rsdp);
bool ParseRsdp(void* rsdp);
void ParseRsdt(AcpiHeader* rsdt);
void ParseXsdt(AcpiHeader* xsdt);
void ParseDT(AcpiHeader* dt);
Expand Down
2 changes: 1 addition & 1 deletion include/interrupts.hpp
Expand Up @@ -74,7 +74,7 @@ typedef bool intcb(uint32_t intr, intcb_regs *regs);

struct intcbreg {
intcb *cb;
intcbreg *prev, *next;
intcbreg *next;
};

class Interrupts {
Expand Down
8 changes: 4 additions & 4 deletions include/memory.hpp
Expand Up @@ -89,7 +89,7 @@ struct ALLOC {
};
struct ALLOCTABLE {
ALLOC allocs[255];
void* next;
ALLOCTABLE* next;
int64_t reserved;
};
extern GRUB *grub_data;
Expand Down Expand Up @@ -120,9 +120,9 @@ class Memory {

inline static void MmioWrite32(void *p, uint32_t data) {
Memory::salloc(p);
*(volatile int *)(p) = data;
*(volatile uint32_t *)(p) = data;
}
inline static uint32_t MmioRead32(void *p) {
inline static uint32_t MmioRead32(const void *p) {
Memory::salloc(p);
return *(volatile int *)(p);
return *(volatile uint32_t *)(p);
}
6 changes: 6 additions & 0 deletions include/pxlib.hpp
Expand Up @@ -103,4 +103,10 @@ extern "C" {
inline static void LeaveCritical(uint64_t flags) {
asm volatile("push %q0; popfq"::"r"(flags));
}

inline static uintptr_t ALIGN(uintptr_t base, size_t align) {
if (base % align == 0)
return base;
return base + align - (base % align);
}
}
2 changes: 1 addition & 1 deletion include/stream.hpp
Expand Up @@ -33,7 +33,7 @@ class MemoryStream: public Stream {
private:
size_t offset;
size_t limit;
void* memory;
char* memory;

public:
MemoryStream(void* memory, size_t limit);
Expand Down
104 changes: 55 additions & 49 deletions src/acpi.cpp
Expand Up @@ -19,8 +19,8 @@ ACPI* ACPI::controller = 0;
uint8_t ACPI::activeCpuCount = 0;
uint64_t ACPI::busfreq = 0;

static const char* ACPI_FIND_START = (const char*)0x000e0000;
static const char* ACPI_FIND_TOP = (const char*)0x000fffff;
static void *const ACPI_FIND_START = reinterpret_cast<char*>(0x000e0000);
static void *const ACPI_FIND_TOP = reinterpret_cast<char*>(0x000fffff);
static const uint64_t ACPI_SIG_RTP_DSR = 0x2052545020445352;
static const uint32_t ACPI_SIG_CIPA = 0x43495041;

Expand All @@ -38,8 +38,8 @@ ACPI* ACPI::getController() {
}

ACPI::ACPI() {
char *p = (char*)ACPI_FIND_START;
char *end = (char*)ACPI_FIND_TOP;
uint64_t *p = static_cast<uint64_t*>(ACPI_FIND_START);
uint64_t *end = static_cast<uint64_t*>(ACPI_FIND_TOP);
acpiCpuCount = 0;
activeCpuCount = 1;

Expand All @@ -50,13 +50,13 @@ ACPI::ACPI() {

while (p < end) {
Memory::salloc(p);
Memory::salloc((uint64_t*)p + 1);
uint64_t signature = *(uint64_t *)p;
Memory::salloc(p + 1);
uint64_t signature = *p;

if ((signature == ACPI_SIG_RTP_DSR) && ParseRsdp(p))
break;

p += 16;
p += 2;
}

outportb(0x61, (inportb(0x61) & 0xFD) | 1);
Expand All @@ -76,91 +76,97 @@ void ACPI::ParseDT(AcpiHeader *header) {
Memory::salloc(header);

if (header->signature == ACPI_SIG_CIPA)
ParseApic((AcpiMadt *)header);
ParseApic(reinterpret_cast<AcpiMadt*>(header));
}
void ACPI::ParseRsdt(AcpiHeader *rsdt) {
Memory::salloc(rsdt);
uint32_t *p = (uint32_t *)(rsdt + 1);
uint32_t *end = (uint32_t *)((char*)rsdt + rsdt->length);
uint32_t *p = reinterpret_cast<uint32_t*>(rsdt + 1);
uint32_t *end = p + (rsdt->length - sizeof(*rsdt)) / sizeof(uint32_t);

while (p < end) {
Memory::salloc(p);
Memory::salloc((uint64_t*)p + 1);
uint64_t address = (uint64_t)((uint32_t)(*p++) & 0xFFFFFFFF);
ParseDT((AcpiHeader *)(uintptr_t)address);
Memory::salloc(p + 1);
uintptr_t address = ((*p++) & 0xFFFFFFFF);
ParseDT(reinterpret_cast<AcpiHeader*>(address));
}
}
void ACPI::ParseXsdt(AcpiHeader *xsdt) {
Memory::salloc(xsdt);
uint64_t *p = (uint64_t *)(xsdt + 1);
uint64_t *end = (uint64_t *)((char*)xsdt + xsdt->length);
uint64_t *p = reinterpret_cast<uint64_t*>(xsdt + 1);
uint64_t *end = p + (xsdt->length - sizeof(*xsdt)) / sizeof(uint64_t);

while (p < end) {
Memory::salloc(p);
Memory::salloc((uint64_t*)p + 1);
Memory::salloc(p + 1);
uint64_t address = *p++;
ParseDT((AcpiHeader *)(uintptr_t)address);
ParseDT(reinterpret_cast<AcpiHeader*>(address));
}
}
void ACPI::ParseApic(AcpiMadt *a_madt) {
Memory::salloc(a_madt);
madt = a_madt;

localApicAddr = (uint32_t *)((uintptr_t)madt->localApicAddr & 0xFFFFFFFF);
localApicAddr = reinterpret_cast<char*>(madt->localApicAddr);
Memory::salloc(localApicAddr);

char *p = (char *)(madt + 1);
char *end = ((char *)madt) + madt->header.length;
uint8_t *p = reinterpret_cast<uint8_t*>(madt + 1);
uint8_t *end = p + (madt->header.length - sizeof(AcpiMadt));

while (p < end) {
ApicHeader *header = (ApicHeader *)p;
ApicHeader *header = reinterpret_cast<ApicHeader*>(p);
Memory::salloc(header);
Memory::salloc(header + 1);
uint16_t type = header->type;
uint16_t length = header->length;
Memory::salloc(header);
if (type == 0) {
ApicLocalApic *s = (ApicLocalApic *)p;
ApicLocalApic *s = reinterpret_cast<ApicLocalApic*>(p);
acpiCpuIds[acpiCpuCount++] = s->apicId;
} else if (type == 1) {
ApicIoApic *s = (ApicIoApic *)p;
ioApicAddr = (uint32_t *)((uintptr_t)s->ioApicAddress & 0xFFFFFFFF);
ApicIoApic *s = reinterpret_cast<ApicIoApic*>(p);
ioApicAddr = reinterpret_cast<char*>(s->ioApicAddress);
Memory::salloc(ioApicAddr);
} else if (type == 2) {
// ApicInterruptOverride *s = (ApicInterruptOverride *)p;
ApicInterruptOverride *s = reinterpret_cast<ApicInterruptOverride*>(p);
(void)s;
// TODO: handle interrupt overrides
}

p += length;
}
}
bool ACPI::ParseRsdp(char *p) {
char sum = 0;
bool ACPI::ParseRsdp(void *ptr) {
uint8_t *p = static_cast<uint8_t*>(ptr);

uint8_t sum = 0;
for (int i = 0; i < 20; ++i)
sum += p[i];

if (sum)
return false;

char oem[7];
Memory::copy(oem, (char*)(p + 9), 6);
oem[6] = '\0';
Memory::copy(oem, p + 9, 6);
oem[6] = 0;

char revision = p[15];
Memory::salloc((int *)(p + 16));
Memory::salloc((int *)(p + 16) + 1);
uint32_t *rsdtPtr = static_cast<uint32_t*>(ptr) + 4;
uint64_t *xsdtPtr = static_cast<uint64_t*>(ptr) + 3;

Memory::salloc(rsdtPtr);
Memory::salloc(rsdtPtr + 1);
uint32_t rsdtAddr = *rsdtPtr;
if (revision == 0) {
int rsdtAddr = *(int *)(p + 16);
ParseRsdt((AcpiHeader *)(uintptr_t)((uint64_t)rsdtAddr & 0xFFFFFFFF));
ParseRsdt(reinterpret_cast<AcpiHeader*>(rsdtAddr));
} else if (revision == 2) {
int rsdtAddr = *(int *)(p + 16);
Memory::salloc((uint64_t *)(p + 24));
Memory::salloc((uint64_t *)(p + 24) + 1);
uint64_t xsdtAddr = *(uint64_t *)(p + 24);
Memory::salloc(xsdtPtr);
Memory::salloc(xsdtPtr + 1);
uint64_t xsdtAddr = *xsdtPtr;

if (xsdtAddr)
ParseXsdt((AcpiHeader *)(uintptr_t)xsdtAddr);
ParseXsdt(reinterpret_cast<AcpiHeader*>(xsdtAddr));
else
ParseRsdt((AcpiHeader *)(uintptr_t)((uint64_t)rsdtAddr & 0xFFFFFFFF));
ParseRsdt(reinterpret_cast<AcpiHeader*>(rsdtAddr));
}

return true;
Expand Down Expand Up @@ -192,33 +198,33 @@ uint32_t ACPI::getCPUIDOfLapic(uint32_t id) {
uint32_t ACPI::LapicIn(uint32_t reg) {
if (!localApicAddr)
return 0;
return MmioRead32((char*)localApicAddr + reg);
return MmioRead32(localApicAddr + reg);
}
void ACPI::LapicOut(uint32_t reg, uint32_t data) {
if (!localApicAddr)
return;
MmioWrite32((char*)localApicAddr + reg, data);
MmioWrite32(localApicAddr + reg, data);
}
uint32_t ACPI::IOapicIn(uint32_t reg) {
if (!ioApicAddr)
return 0;
MmioWrite32((char*)ioApicAddr + IOAPIC_REGSEL, reg);
return MmioRead32((char*)ioApicAddr + IOAPIC_REGWIN);
MmioWrite32(ioApicAddr + IOAPIC_REGSEL, reg);
return MmioRead32(ioApicAddr + IOAPIC_REGWIN);
}
void ACPI::IOapicOut(uint32_t reg, uint32_t data) {
if (!ioApicAddr)
return;
MmioWrite32((char*)ioApicAddr + IOAPIC_REGSEL, reg);
MmioWrite32((char*)ioApicAddr + IOAPIC_REGWIN, data);
MmioWrite32(ioApicAddr + IOAPIC_REGSEL, reg);
MmioWrite32(ioApicAddr + IOAPIC_REGWIN, data);
}
union ioapic_redir_ints {
ioapic_redir r;
int i[2];
uint32_t i[2];
};
void ACPI::IOapicMap(uint32_t idx, ioapic_redir r) {
ioapic_redir_ints *a = (ioapic_redir_ints *)&r;
IOapicOut(IOAPIC_REDTBL + idx * 2 + 0, a->i[0]);
IOapicOut(IOAPIC_REDTBL + idx * 2 + 1, a->i[1]);
ioapic_redir_ints a = { .r = r };
IOapicOut(IOAPIC_REDTBL + idx * 2 + 0, a.i[0]);
IOapicOut(IOAPIC_REDTBL + idx * 2 + 1, a.i[1]);
}
ioapic_redir ACPI::IOapicReadMap(uint32_t idx) {
ioapic_redir_ints a;
Expand Down
26 changes: 9 additions & 17 deletions src/cpu.cpp
Expand Up @@ -90,14 +90,10 @@ char* CPU::getVendor() {
asm volatile("cpuid" :
"=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) :
"a"(eax));
union u {
char s[12];
int i[3];
};
u *v = (u*)&vendor;
v->i[0] = ebx;
v->i[1] = edx;
v->i[2] = ecx;
uint32_t *v = reinterpret_cast<uint32_t*>(vendor);
v[0] = ebx;
v[1] = edx;
v[2] = ecx;
vendor[12] = 0;
}
return vendor;
Expand Down Expand Up @@ -186,7 +182,7 @@ char* CPU::getFeaturesStr() {
uint32_t count = bitcnt(f) + bitcnt(ef) + bitcnt(fe);

size_t bufsize = 17 * count;
char *buf = (char*)alloca(bufsize);
char *buf = static_cast<char*>(alloca(bufsize));
char *end = buf;

for (int i = 0; i < 64; i++) {
Expand Down Expand Up @@ -216,26 +212,22 @@ char* CPU::getFeaturesStr() {

char* CPU::getBrandString() {
if ((brandString[0] == 0) && (getMaxCPUID() >= 0x80000004)) {
union u {
uint32_t i[12];
char s[48];
};
u *v = (u*)brandString;
uint32_t *v = reinterpret_cast<uint32_t*>(brandString);
uint32_t eax = 0x80000002, ebx, ecx, edx;
asm volatile("cpuid" :
"=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) :
"a"(eax));
v->i[0] = eax, v->i[1] = ebx, v->i[2] = ecx, v->i[3] = edx;
v[0] = eax, v[1] = ebx, v[2] = ecx, v[3] = edx;
eax = 0x80000003;
asm volatile("cpuid" :
"=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) :
"a"(eax));
v->i[4] = eax, v->i[5] = ebx, v->i[6] = ecx, v->i[7] = edx;
v[4] = eax, v[5] = ebx, v[6] = ecx, v[7] = edx;
eax = 0x80000004;
asm volatile("cpuid" :
"=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) :
"a"(eax));
v->i[8] = eax, v->i[9] = ebx, v->i[10] = ecx, v->i[11] = edx;
v[8] = eax, v[9] = ebx, v[10] = ecx, v[11] = edx;
brandString[48] = 0;
}
return brandString;
Expand Down

0 comments on commit 27cdf0f

Please sign in to comment.