fix pit_busy_wait; better tasking

main
Clyne 3 months ago
parent 35aa65037b
commit bd0acf8836
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -10,6 +10,8 @@
static VGATerminal vga; static VGATerminal vga;
TextOutput& term = vga; TextOutput& term = vga;
static volatile bool termBusy = false;
void kernel_main(void) void kernel_main(void)
{ {
term.write("Clyne's kernel, v2024\n\n"); term.write("Clyne's kernel, v2024\n\n");
@ -28,18 +30,41 @@ void kernel_main(void)
gdt_initialize(); gdt_initialize();
pic_initialize(); pic_initialize();
idt_initialize(); idt_initialize();
pit_initialize(50); pit_initialize(100);
asm volatile("sti"); asm volatile("sti");
tasking_initialize(); tasking_initialize();
term.write("Tasking enabled.\n"); term.write("Tasking enabled.\n");
tasking_spawn([] { tasking_spawn([] {
for (;;) for (;;) {
do pit_busy_wait(1);
while (termBusy);
termBusy = true;
term.write('B'); term.write('B');
termBusy = false;
}
}, 256);
tasking_spawn([] {
for (;;) {
do pit_busy_wait(1);
while (termBusy);
termBusy = true;
term.write('C');
termBusy = false;
}
}, 256); }, 256);
for (;;) for (;;) {
do pit_busy_wait(1);
while (termBusy);
termBusy = true;
term.write('A'); term.write('A');
termBusy = false;
}
} }
extern "C" extern "C"

@ -33,7 +33,7 @@ void pit_initialize(std::uint32_t frequency)
void pit_busy_wait(std::int32_t tks) void pit_busy_wait(std::int32_t tks)
{ {
const auto end = ticks + tks; const auto end = ticks + tks;
while (end - ticks > 0) while (static_cast<std::int32_t>(end - ticks) > 0)
asm volatile("nop"); asm volatile("nop");
} }

@ -7,6 +7,7 @@ struct Task
enum class State { enum class State {
Invalid, Invalid,
Staging, Staging,
Staged,
Running Running
}; };
@ -33,14 +34,14 @@ void schedule(const Registers& regs)
do { do {
if (++current >= tasks.size()) if (++current >= tasks.size())
current = 0; current = 0;
} while (tasks[current].state == Task::Invalid); } while (tasks[current].state == Task::Invalid || tasks[current].state == Task::Staging);
asm volatile(R"( asm volatile(R"(
mov %0, %%esp mov %0, %%esp
mov %1, %%ebp mov %1, %%ebp
)" :: "m" (tasks[current].esp), "m" (tasks[current].ebp)); )" :: "m" (tasks[current].esp), "m" (tasks[current].ebp));
if (tasks[current].state == Task::Staging) { if (tasks[current].state == Task::Staged) {
tasks[current].state = Task::Running; tasks[current].state = Task::Running;
asm volatile(R"( asm volatile(R"(
pop %eax pop %eax
@ -69,7 +70,7 @@ bool tasking_spawn(void (*entry)(), unsigned ssize)
if (i < 0) if (i < 0)
return false; return false;
tasks[i] = Task(); tasks[i].state = Task::Staging;
auto stack = reinterpret_cast<std::uint32_t>(new std::uint8_t[ssize]); auto stack = reinterpret_cast<std::uint32_t>(new std::uint8_t[ssize]);
const auto stackend = stack + ssize; const auto stackend = stack + ssize;
@ -81,9 +82,11 @@ bool tasking_spawn(void (*entry)(), unsigned ssize)
r->cs = 0x8; r->cs = 0x8;
asm volatile("pushfl; pop %%eax" : "=a"(r->eflags)); asm volatile("pushfl; pop %%eax" : "=a"(r->eflags));
tasks[i].esp = regbase; tasks[i] = Task {
tasks[i].ebp = stackend; .esp = regbase,
tasks[i].state = Task::Staging; .ebp = stackend,
.state = Task::Staged
};
return true; return true;
} }

@ -46,9 +46,11 @@ void VGATerminal::checkpos() noexcept
void VGATerminal::updatecursor() const noexcept void VGATerminal::updatecursor() const noexcept
{ {
asm volatile("cli");
outb(0x03d4, 0x0f); outb(0x03d4, 0x0f);
outb(0x03d5, static_cast<std::uint8_t>(offset)); outb(0x03d5, static_cast<std::uint8_t>(offset));
outb(0x03d4, 0x0e); outb(0x03d4, 0x0e);
outb(0x03d5, static_cast<std::uint8_t>(offset >> 8)); outb(0x03d5, static_cast<std::uint8_t>(offset >> 8));
asm volatile("sti");
} }

Loading…
Cancel
Save