Skip to content

Commit e3e277c

Browse files
committed
Kind of awful, really
1 parent 08115e5 commit e3e277c

File tree

8 files changed

+82
-51
lines changed

8 files changed

+82
-51
lines changed

include/Cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace Thorn {
5252
}
5353

5454
ensureSpace(1);
55-
auto [iter, inserted] = map.try_emplace(key, std::bit_cast<void *>(list.end()), std::move(value));
55+
auto [iter, inserted] = map.try_emplace(key, nullptr, std::move(value));
5656
assert(inserted);
5757
list.push_front(iter);
5858
iter->second.first = std::bit_cast<void *>(list.begin());

include/hardware/AHCI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,11 @@ namespace Thorn::AHCI {
365365
public:
366366
constexpr static size_t BLOCKSIZE = 512;
367367

368+
Controller *parent = nullptr;
368369
volatile HBAPort *registers = nullptr;
369370
volatile HBAMemory *abar = nullptr;
370371
volatile HBACommandHeader *commandList = nullptr;
371372
volatile HBAFIS *fis = nullptr;
372-
Controller *parent = nullptr;
373373
volatile HBACommandTable *commandTables[8];
374374
void *physicalBuffers[8];
375375
Status status = Status::Uninitialized;

include/hardware/PCIIDs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#include <stdint.h>
1+
#pragma once
2+
3+
#include <cstdint>
24

35
namespace Thorn::PCI::ID {
46
static constexpr const char *vendor_0001 = "SafeNet (wrong ID)";

src/arch/x86_64/Interrupts.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#include "arch/x86_64/control_register.h"
33
#include "arch/x86_64/CPU.h"
44
#include "arch/x86_64/Interrupts.h"
5-
#include "arch/x86_64/PageMeta.h"
6-
#include "arch/x86_64/PageTableWrapper.h"
75
#include "arch/x86_64/PIC.h"
86
#include "hardware/Ports.h"
97
#include "hardware/PS2Keyboard.h"
@@ -17,6 +15,11 @@
1715
// #define DEBUG_PAGE_FAULTS
1816
#define DEADLY_PAGE_FAULTS
1917

18+
#ifdef DEBUG_PAGE_FAULTS
19+
#include "arch/x86_64/PageMeta.h"
20+
#include "arch/x86_64/PageTableWrapper.h"
21+
#endif
22+
2023
extern bool irqInvoked;
2124

2225
bool abouttodie = false;

src/device/AHCIDevice.cpp

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace Thorn {
66
int AHCIDevice::read(void *buffer, size_t size, size_t offset) {
7-
if (offset % AHCI::Port::BLOCKSIZE == 0 && size % AHCI::Port::BLOCKSIZE == 0)
8-
return static_cast<int>(port->read(offset / AHCI::Port::BLOCKSIZE, size, buffer));
9-
return static_cast<int>(port->readBytes(size, offset, buffer));
7+
// if (offset % AHCI::Port::BLOCKSIZE == 0 && size % AHCI::Port::BLOCKSIZE == 0)
8+
// return static_cast<int>(port->read(offset / AHCI::Port::BLOCKSIZE, size, buffer));
9+
// return static_cast<int>(port->readBytes(size, offset, buffer));
10+
return readCache(buffer, size, offset);
1011
}
1112

1213
int AHCIDevice::write(const void *buffer, size_t size, size_t offset) {
@@ -17,45 +18,17 @@ namespace Thorn {
1718
}
1819

1920
int AHCIDevice::clear(size_t offset, size_t size) {
20-
if (offset % AHCI::Port::BLOCKSIZE == 0 && size % AHCI::Port::BLOCKSIZE == 0) {
21-
char buffer[AHCI::Port::BLOCKSIZE] = {0};
22-
AHCI::Port::AccessStatus status = AHCI::Port::AccessStatus::Success;
23-
size_t last_pct = 0;
24-
printf("0%%\n");
25-
for (size_t i = 0, max = size / AHCI::Port::BLOCKSIZE; i < max; ++i) {
26-
status = port->write(offset / AHCI::Port::BLOCKSIZE + i, AHCI::Port::BLOCKSIZE, buffer);
27-
if (status != AHCI::Port::AccessStatus::Success) {
28-
printf("[%d] Whoops: %d\n", __LINE__, status);
29-
return static_cast<int>(status);
30-
}
31-
32-
if (size_t new_pct = i * 100 / max; new_pct != last_pct) {
33-
last_pct = new_pct;
34-
serprintf("\e[F");
35-
printf("%lu%%\n", new_pct);
36-
}
37-
}
38-
printf("[%d] Whoops: %d\n", __LINE__, status);
39-
return static_cast<int>(status);
40-
}
41-
42-
size_t size_copy = size;
43-
size_t chunk_size = 1;
44-
while ((size_copy & 1) == 0 && chunk_size < AHCI::Port::BLOCKSIZE) {
45-
size_copy /= 2;
46-
chunk_size *= 2;
47-
}
21+
int status{};
22+
char zeroes[BlockSize];
4823

49-
auto buffer = std::make_unique<char[]>(chunk_size);
50-
for (size_t i = 0, max = size / chunk_size; i < max; ++i) {
51-
AHCI::Port::AccessStatus status = port->writeBytes(chunk_size, offset + chunk_size * i, buffer.get());
52-
if (status != AHCI::Port::AccessStatus::Success) {
53-
printf("[%d] Whoops: %d\n", __LINE__, status);
54-
return static_cast<int>(status);
55-
}
24+
while (size > BlockSize) {
25+
if (0 != (status = write(zeroes, BlockSize, offset)))
26+
return status;
27+
offset += BlockSize;
28+
size -= BlockSize;
5629
}
5730

58-
return 0;
31+
return write(zeroes, size, offset);
5932
}
6033

6134
std::string AHCIDevice::getName() const {
@@ -84,6 +57,56 @@ namespace Thorn {
8457
entry.flushed = true;
8558
}
8659

60+
int AHCIDevice::readCache(void *buffer, size_t size, size_t offset) {
61+
int status{};
62+
63+
auto advance = [&](size_t amount) {
64+
offset += amount;
65+
buffer = reinterpret_cast<char *>(buffer) + amount;
66+
size -= amount;
67+
return size == 0;
68+
};
69+
70+
if (size_t rem = offset % BlockSize; rem != 0) {
71+
auto [ref, created] = cache[offset / BlockSize];
72+
StorageCacheEntry<BlockSize> &entry = ref.get();
73+
74+
if (created) {
75+
if (0 != (status = read(entry.data, BlockSize, offset - rem)))
76+
return status;
77+
entry.flushed = true;
78+
}
79+
80+
size_t affected = std::min(size, BlockSize - rem);
81+
std::memmove(buffer, &entry.data[rem], affected);
82+
if (advance(affected))
83+
return 0;
84+
}
85+
86+
while (size >= BlockSize) {
87+
auto [ref, created] = cache[offset / BlockSize];
88+
StorageCacheEntry<BlockSize> &entry = ref.get();
89+
entry.flushed = false;
90+
std::memmove(buffer, entry.data, BlockSize);
91+
if (advance(BlockSize))
92+
return 0;
93+
}
94+
95+
assert(size > 0);
96+
97+
auto [ref, created] = cache[offset / BlockSize];
98+
StorageCacheEntry<BlockSize> &entry = ref.get();
99+
100+
if (created) {
101+
if (0 != (status = read(entry.data, size, offset)))
102+
return status;
103+
entry.flushed = true;
104+
}
105+
106+
std::memmove(buffer, entry.data, size);
107+
return 0;
108+
}
109+
87110
int AHCIDevice::writeCache(const void *buffer, size_t size, size_t offset) {
88111
int status{};
89112

src/hardware/AHCI.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ namespace Thorn::AHCI {
179179
cfis->lba3 = 0;
180180
cfis->lba4 = 0;
181181
cfis->lba5 = 0;
182-
cfis->countLow = cfis->countHigh = 0;
182+
cfis->countLow = 0;
183+
cfis->countHigh = 0;
183184
cfis->control = 0;
184185

185186
spin = SPIN_COUNT;
@@ -679,7 +680,7 @@ namespace Thorn::AHCI {
679680
return status;
680681
const size_t to_write = (BLOCKSIZE - offset) < count? BLOCKSIZE - offset : count;
681682
memcpy(write_buffer + offset, cbuffer, to_write);
682-
if ((status = write(lba, BLOCKSIZE, write_buffer) ) != AccessStatus::Success)
683+
if ((status = write(lba, BLOCKSIZE, write_buffer)) != AccessStatus::Success)
683684
return status;
684685
count -= to_write;
685686
++lba;

src/hardware/PCI.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ namespace Thorn::PCI {
265265

266266
if (vector & static_cast<uint8_t>(Vector::Legacy)) {
267267
const uint8_t irq = getInterruptLine();
268-
if (irq == 0xff) {
268+
if (irq == 0xff) {
269269
printf("[PCI::Device::allocateVector] Legacy interrupts aren't supported by device.\n");
270270
return 0xff;
271271
} else {
@@ -387,6 +387,7 @@ namespace Thorn::PCI {
387387
else
388388
printf("%lu %x:%x:%x Vendor: %x, device: %x, class: %x, subclass: %x ",
389389
device_count++, bus, device, function, vendor, device_id, baseclass, subclass);
390+
390391
if (Serial::ready)
391392
Serial::write('\n');
392393

src/memory/Memory.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,19 @@ namespace Thorn {
4646
#ifdef DEBUG_ALLOCATION
4747
printf("requestSpace(0x%lx, %lu)\n", last, size);
4848
#endif
49-
BlockMeta *block = (BlockMeta *) realign((uintptr_t) end);
49+
BlockMeta *block = reinterpret_cast<BlockMeta *>(realign(uintptr_t(end)));
5050

5151
if (last)
5252
last->next = block;
5353

54+
end = reinterpret_cast<char *>(block) + size + sizeof(BlockMeta) + 1;
55+
5456
#ifdef PROACTIVE_PAGING
5557
{
5658
Lock<Mutex> pager_lock;
5759
auto &pager = Kernel::getPager(pager_lock);
5860
auto &wrapper = Kernel::instance->kernelPML4;
59-
while (highestAllocated <= (uintptr_t) block + size) {
61+
while (highestAllocated <= uintptr_t(end)) {
6062
pager.assignAddress(wrapper, highestAllocated);
6163
highestAllocated += PAGE_LENGTH;
6264
}
@@ -65,9 +67,8 @@ namespace Thorn {
6567

6668
block->size = size;
6769
block->next = nullptr;
68-
block->free = 0;
70+
block->free = false;
6971

70-
end = reinterpret_cast<char *>(block) + block->size + sizeof(BlockMeta) + 1;
7172
return block;
7273
}
7374

0 commit comments

Comments
 (0)