Compare commits
25 Commits
e73af4baa8
...
7541540fd3
Author | SHA1 | Date | |
---|---|---|---|
7541540fd3 | |||
c477f85c19 | |||
dceb6be559 | |||
9500d14087 | |||
9f9b9a17bc | |||
62802f6e1c | |||
662d061f89 | |||
3315a79de0 | |||
92bff6deb4 | |||
d8c30c40a3 | |||
e5e2a7bda6 | |||
52dd5501f3 | |||
f50e153313 | |||
ffb70932f3 | |||
25bcba413f | |||
064ff307dc | |||
454a586296 | |||
67e7633ffd | |||
c3c0fc71f1 | |||
3e88a5a689 | |||
9f7dc7ae79 | |||
3db0c4fed2 | |||
1506331872 | |||
0dbfbe6395 | |||
84bed3ceb3 |
@ -26,3 +26,4 @@ luna_app(login.cpp login)
|
|||||||
luna_app(ipc-test.cpp ipc-test)
|
luna_app(ipc-test.cpp ipc-test)
|
||||||
luna_app(mount.cpp mount)
|
luna_app(mount.cpp mount)
|
||||||
luna_app(umount.cpp umount)
|
luna_app(umount.cpp umount)
|
||||||
|
luna_app(ps.cpp ps)
|
||||||
|
@ -40,6 +40,8 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
gid = grp->gr_gid;
|
gid = grp->gr_gid;
|
||||||
|
|
||||||
|
endgrent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -73,6 +75,8 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
|
|
||||||
gid = pw->pw_gid;
|
gid = pw->pw_gid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
endpwent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
44
apps/ps.cpp
Normal file
44
apps/ps.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <os/ArgumentParser.h>
|
||||||
|
#include <os/File.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <sys/pstat.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
Result<int> luna_main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
os::ArgumentParser parser;
|
||||||
|
parser.add_description("Show a list of processes on the system.");
|
||||||
|
parser.add_system_program_info("ps"_sv);
|
||||||
|
parser.parse(argc, argv);
|
||||||
|
|
||||||
|
os::println("UID PID PPID TIME CMD");
|
||||||
|
|
||||||
|
pid_t last = pstat(-1, nullptr);
|
||||||
|
for (pid_t pid = 1; pid <= last; pid++)
|
||||||
|
{
|
||||||
|
static process ps;
|
||||||
|
if (pstat(pid, &ps) < 0)
|
||||||
|
{
|
||||||
|
if (errno == ESRCH) continue;
|
||||||
|
return err(errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct tm tm;
|
||||||
|
gmtime_r(&ps.ps_time.tv_sec, &tm);
|
||||||
|
|
||||||
|
char timebuf[256];
|
||||||
|
strftime(timebuf, sizeof(timebuf), "%H:%M:%S", &tm);
|
||||||
|
|
||||||
|
const char* user = "???";
|
||||||
|
|
||||||
|
passwd* pw = getpwuid(ps.ps_uid);
|
||||||
|
if (pw) user = pw->pw_name;
|
||||||
|
|
||||||
|
os::println("%-8s %6d %6d %10s %s", user, ps.ps_pid, ps.ps_ppid, timebuf, ps.ps_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
endpwent();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -74,6 +74,7 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
struct passwd* pw = getpwuid(getuid());
|
struct passwd* pw = getpwuid(getuid());
|
||||||
if (pw) { username = pw->pw_name; }
|
if (pw) { username = pw->pw_name; }
|
||||||
else { username = getenv("USER"); }
|
else { username = getenv("USER"); }
|
||||||
|
endpwent();
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
|
@ -78,6 +78,8 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
endpwent();
|
||||||
|
|
||||||
if ((prompt_password || getuid() != geteuid()) && *entry->pw_passwd)
|
if ((prompt_password || getuid() != geteuid()) && *entry->pw_passwd)
|
||||||
{
|
{
|
||||||
char* pass = getpass();
|
char* pass = getpass();
|
||||||
|
@ -31,7 +31,7 @@ set(SOURCES
|
|||||||
src/sys/file.cpp
|
src/sys/file.cpp
|
||||||
src/sys/id.cpp
|
src/sys/id.cpp
|
||||||
src/sys/mkdir.cpp
|
src/sys/mkdir.cpp
|
||||||
src/sys/mknod.cpp
|
src/sys/pstat.cpp
|
||||||
src/sys/waitpid.cpp
|
src/sys/waitpid.cpp
|
||||||
src/sys/getdents.cpp
|
src/sys/getdents.cpp
|
||||||
src/sys/stat.cpp
|
src/sys/stat.cpp
|
||||||
@ -63,6 +63,7 @@ if("${LUNA_ARCH}" MATCHES "x86_64")
|
|||||||
src/arch/x86_64/Thread.cpp
|
src/arch/x86_64/Thread.cpp
|
||||||
src/arch/x86_64/PCI.cpp
|
src/arch/x86_64/PCI.cpp
|
||||||
src/arch/x86_64/Keyboard.cpp
|
src/arch/x86_64/Keyboard.cpp
|
||||||
|
src/arch/x86_64/disk/ATA.cpp
|
||||||
src/arch/x86_64/init/GDT.cpp
|
src/arch/x86_64/init/GDT.cpp
|
||||||
src/arch/x86_64/init/IDT.cpp
|
src/arch/x86_64/init/IDT.cpp
|
||||||
src/arch/x86_64/init/PIC.cpp
|
src/arch/x86_64/init/PIC.cpp
|
||||||
|
@ -22,6 +22,9 @@ namespace CPU
|
|||||||
void disable_interrupts();
|
void disable_interrupts();
|
||||||
void wait_for_interrupt();
|
void wait_for_interrupt();
|
||||||
|
|
||||||
|
bool save_interrupts();
|
||||||
|
void restore_interrupts(bool saved);
|
||||||
|
|
||||||
void get_stack_trace(void (*callback)(u64, void*), void* arg);
|
void get_stack_trace(void (*callback)(u64, void*), void* arg);
|
||||||
void print_stack_trace();
|
void print_stack_trace();
|
||||||
void get_stack_trace_at(Registers* regs, void (*callback)(u64, void*), void* arg);
|
void get_stack_trace_at(Registers* regs, void (*callback)(u64, void*), void* arg);
|
||||||
@ -29,5 +32,8 @@ namespace CPU
|
|||||||
|
|
||||||
u16 get_processor_id();
|
u16 get_processor_id();
|
||||||
|
|
||||||
|
bool register_interrupt(u8 interrupt, void (*handler)(Registers*, void*), void* context);
|
||||||
|
void sync_interrupts();
|
||||||
|
|
||||||
void pause();
|
void pause();
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,10 @@ struct ScanInfo
|
|||||||
|
|
||||||
namespace PCI
|
namespace PCI
|
||||||
{
|
{
|
||||||
|
BAR::BAR(u32 raw) : m_raw(raw)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
Device::ID read_id(const Device::Address& address)
|
Device::ID read_id(const Device::Address& address)
|
||||||
{
|
{
|
||||||
const u16 vendor = read16(address, Field::VendorID);
|
const u16 vendor = read16(address, Field::VendorID);
|
||||||
@ -108,4 +112,13 @@ namespace PCI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BAR Device::getBAR(u8 index) const
|
||||||
|
{
|
||||||
|
check(index < 6);
|
||||||
|
|
||||||
|
u32 raw = read32(address, 0x10 + (index * 4));
|
||||||
|
|
||||||
|
return { raw };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,67 @@ namespace PCI
|
|||||||
|
|
||||||
HeaderType = 0x0e,
|
HeaderType = 0x0e,
|
||||||
|
|
||||||
SecondaryBus = 0x19
|
BAR0 = 0x10,
|
||||||
|
BAR1 = 0x14,
|
||||||
|
BAR2 = 0x18,
|
||||||
|
BAR3 = 0x1c,
|
||||||
|
|
||||||
|
SecondaryBus = 0x19,
|
||||||
|
|
||||||
|
InterruptLine = 0x3c,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum CommandField : u16
|
||||||
|
{
|
||||||
|
CMD_IO_SPACE = 1 << 0,
|
||||||
|
CMD_MEMORY_SPACE = 1 << 1,
|
||||||
|
CMD_BUS_MASTER = 1 << 2,
|
||||||
|
CMD_SPECIAL_CYCLES = 1 << 3,
|
||||||
|
CMD_MEMORY_WRITE_AND_INVALIDATE = 1 << 4,
|
||||||
|
CMD_VGA_PALETTE_SNOOP = 1 << 5,
|
||||||
|
CMD_PARITY_ERROR_RESPONSE = 1 << 6,
|
||||||
|
CMD_SERR = 1 << 8,
|
||||||
|
CMD_FAST_BACK_TO_BACK = 1 << 9,
|
||||||
|
CMD_INTERRUPT_DISABLE = 1 << 10,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct BAR
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BAR(u32 raw);
|
||||||
|
|
||||||
|
bool is_iospace()
|
||||||
|
{
|
||||||
|
return m_raw & 0x01;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_memory_space()
|
||||||
|
{
|
||||||
|
return !is_iospace();
|
||||||
|
}
|
||||||
|
|
||||||
|
u16 port()
|
||||||
|
{
|
||||||
|
return (u16)(m_raw & 0xfffffffc);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 type()
|
||||||
|
{
|
||||||
|
return (m_raw >> 1) & 0x03;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_prefetchable()
|
||||||
|
{
|
||||||
|
return m_raw & (1 << 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 address_32bit()
|
||||||
|
{
|
||||||
|
return m_raw & 0xFFFFFFF0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
u32 m_raw;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Device
|
struct Device
|
||||||
@ -42,6 +102,8 @@ namespace PCI
|
|||||||
u32 function;
|
u32 function;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BAR getBAR(u8 index) const;
|
||||||
|
|
||||||
ID id;
|
ID id;
|
||||||
Type type;
|
Type type;
|
||||||
Address address;
|
Address address;
|
||||||
|
@ -188,4 +188,18 @@ ISR_ERROR 21 ; control-protection exception (#CP)
|
|||||||
; ISR 22-31 reserved
|
; ISR 22-31 reserved
|
||||||
IRQ 32, 0 ; timer interrupt
|
IRQ 32, 0 ; timer interrupt
|
||||||
IRQ 33, 1 ; keyboard interrupt
|
IRQ 33, 1 ; keyboard interrupt
|
||||||
|
IRQ 34, 2
|
||||||
|
IRQ 35, 3
|
||||||
|
IRQ 36, 4
|
||||||
|
IRQ 37, 5
|
||||||
|
IRQ 38, 6
|
||||||
|
IRQ 39, 7
|
||||||
|
IRQ 40, 8
|
||||||
|
IRQ 41, 9
|
||||||
|
IRQ 42, 10
|
||||||
|
IRQ 43, 11
|
||||||
|
IRQ 44, 12
|
||||||
|
IRQ 45, 13
|
||||||
|
IRQ 46, 14
|
||||||
|
IRQ 47, 15
|
||||||
ISR 66 ; system call
|
ISR 66 ; system call
|
||||||
|
@ -22,12 +22,23 @@ extern "C" void enable_nx();
|
|||||||
|
|
||||||
extern void setup_gdt();
|
extern void setup_gdt();
|
||||||
extern void remap_pic();
|
extern void remap_pic();
|
||||||
|
extern void change_pic_masks(u8 pic1_mask, u8 pic2_mask);
|
||||||
extern void pic_eoi(unsigned char irq);
|
extern void pic_eoi(unsigned char irq);
|
||||||
extern void pic_eoi(Registers* regs);
|
extern void pic_eoi(Registers* regs);
|
||||||
extern void setup_idt();
|
extern void setup_idt();
|
||||||
|
|
||||||
static Thread* g_io_thread;
|
static Thread* g_io_thread;
|
||||||
|
|
||||||
|
typedef void (*interrupt_handler_t)(Registers*, void*);
|
||||||
|
|
||||||
|
struct InterruptHandler
|
||||||
|
{
|
||||||
|
interrupt_handler_t function;
|
||||||
|
void* context;
|
||||||
|
};
|
||||||
|
|
||||||
|
static InterruptHandler irq_handlers[16];
|
||||||
|
|
||||||
void FPData::save()
|
void FPData::save()
|
||||||
{
|
{
|
||||||
asm volatile("fxsave (%0)" : : "r"(m_data));
|
asm volatile("fxsave (%0)" : : "r"(m_data));
|
||||||
@ -135,21 +146,34 @@ void io_thread()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called from _asm_interrupt_entry
|
static void timer_interrupt(Registers* regs, void*)
|
||||||
extern "C" void arch_interrupt_entry(Registers* regs)
|
|
||||||
{
|
|
||||||
if (regs->isr < 32) handle_x86_exception(regs);
|
|
||||||
else if (regs->isr == 32) // Timer interrupt
|
|
||||||
{
|
{
|
||||||
Timer::tick();
|
Timer::tick();
|
||||||
if (should_invoke_scheduler()) Scheduler::invoke(regs);
|
if (should_invoke_scheduler()) Scheduler::invoke(regs);
|
||||||
pic_eoi(regs);
|
|
||||||
}
|
}
|
||||||
else if (regs->isr == 33) // Keyboard interrupt
|
|
||||||
|
static void keyboard_interrupt(Registers*, void*)
|
||||||
{
|
{
|
||||||
u8 scancode = IO::inb(0x60);
|
u8 scancode = IO::inb(0x60);
|
||||||
scancode_queue.try_push(scancode);
|
scancode_queue.try_push(scancode);
|
||||||
g_io_thread->wake_up();
|
g_io_thread->wake_up();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Called from _asm_interrupt_entry
|
||||||
|
extern "C" void arch_interrupt_entry(Registers* regs)
|
||||||
|
{
|
||||||
|
if (regs->isr < 32) handle_x86_exception(regs);
|
||||||
|
else if (regs->isr >= 32 && regs->isr < 48) // IRQ from the PIC
|
||||||
|
{
|
||||||
|
u64 irq = regs->error;
|
||||||
|
auto handler = irq_handlers[irq];
|
||||||
|
if (!handler.function)
|
||||||
|
{
|
||||||
|
kwarnln("Unhandled IRQ catched! Halting.");
|
||||||
|
CPU::efficient_halt();
|
||||||
|
}
|
||||||
|
|
||||||
|
handler.function(regs, handler.context);
|
||||||
pic_eoi(regs);
|
pic_eoi(regs);
|
||||||
}
|
}
|
||||||
else if (regs->isr == 66) // System call
|
else if (regs->isr == 66) // System call
|
||||||
@ -159,7 +183,7 @@ extern "C" void arch_interrupt_entry(Registers* regs)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
kwarnln("IRQ catched! Halting.");
|
kwarnln("Unhandled interrupt catched! Halting.");
|
||||||
CPU::efficient_halt();
|
CPU::efficient_halt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -218,6 +242,10 @@ namespace CPU
|
|||||||
kwarnln("not setting the NX bit as it is unsupported");
|
kwarnln("not setting the NX bit as it is unsupported");
|
||||||
setup_gdt();
|
setup_gdt();
|
||||||
setup_idt();
|
setup_idt();
|
||||||
|
|
||||||
|
memset(irq_handlers, 0, sizeof(irq_handlers));
|
||||||
|
register_interrupt(0, timer_interrupt, nullptr);
|
||||||
|
register_interrupt(1, keyboard_interrupt, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void platform_finish_init()
|
void platform_finish_init()
|
||||||
@ -226,6 +254,8 @@ namespace CPU
|
|||||||
.expect_value("Could not create the IO background thread!");
|
.expect_value("Could not create the IO background thread!");
|
||||||
|
|
||||||
remap_pic();
|
remap_pic();
|
||||||
|
|
||||||
|
sync_interrupts();
|
||||||
}
|
}
|
||||||
|
|
||||||
void enable_interrupts()
|
void enable_interrupts()
|
||||||
@ -238,6 +268,20 @@ namespace CPU
|
|||||||
asm volatile("cli");
|
asm volatile("cli");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool save_interrupts()
|
||||||
|
{
|
||||||
|
u64 flags;
|
||||||
|
asm volatile("pushfq; pop %0" : "=r"(flags));
|
||||||
|
return flags & 0x200;
|
||||||
|
}
|
||||||
|
|
||||||
|
void restore_interrupts(bool saved)
|
||||||
|
{
|
||||||
|
if (saved) enable_interrupts();
|
||||||
|
else
|
||||||
|
disable_interrupts();
|
||||||
|
}
|
||||||
|
|
||||||
void wait_for_interrupt()
|
void wait_for_interrupt()
|
||||||
{
|
{
|
||||||
asm volatile("hlt");
|
asm volatile("hlt");
|
||||||
@ -338,6 +382,35 @@ namespace CPU
|
|||||||
__get_cpuid(1, &unused, &ebx, &unused, &unused);
|
__get_cpuid(1, &unused, &ebx, &unused, &unused);
|
||||||
return (u16)(ebx >> 24);
|
return (u16)(ebx >> 24);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool register_interrupt(u8 interrupt, interrupt_handler_t handler, void* context)
|
||||||
|
{
|
||||||
|
if (irq_handlers[interrupt].function) return false;
|
||||||
|
|
||||||
|
irq_handlers[interrupt] = { handler, context };
|
||||||
|
|
||||||
|
sync_interrupts();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sync_interrupts()
|
||||||
|
{
|
||||||
|
u8 pic1_mask, pic2_mask;
|
||||||
|
pic1_mask = pic2_mask = 0b11111111;
|
||||||
|
for (int i = 0; i < 8; i++)
|
||||||
|
{
|
||||||
|
if (irq_handlers[i].function) pic1_mask &= (u8)(~(1 << i));
|
||||||
|
if (irq_handlers[i + 8].function) pic2_mask &= (u8)(~(1 << i));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pic2_mask != 0b11111111) pic1_mask &= 0b11111011;
|
||||||
|
|
||||||
|
auto val = CPU::save_interrupts();
|
||||||
|
CPU::disable_interrupts();
|
||||||
|
change_pic_masks(pic1_mask, pic2_mask);
|
||||||
|
CPU::restore_interrupts(val);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// called by kernel_yield
|
// called by kernel_yield
|
||||||
|
@ -33,19 +33,32 @@ namespace PCI
|
|||||||
|
|
||||||
void write8(const Device::Address& address, u32 field, u8 value)
|
void write8(const Device::Address& address, u32 field, u8 value)
|
||||||
{
|
{
|
||||||
ignore(address, field, value);
|
u8 offset = (u8)(field & ~0x3);
|
||||||
todo();
|
union {
|
||||||
|
u8 split[4];
|
||||||
|
u32 full;
|
||||||
|
};
|
||||||
|
full = read32(address, offset);
|
||||||
|
split[(field & 0x3)] = value;
|
||||||
|
write32(address, offset, full);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write16(const Device::Address& address, u32 field, u8 value)
|
void write16(const Device::Address& address, u32 field, u16 value)
|
||||||
{
|
{
|
||||||
ignore(address, field, value);
|
u8 offset = (u8)(field & ~0x3);
|
||||||
todo();
|
union {
|
||||||
|
u8 split[4];
|
||||||
|
u32 full;
|
||||||
|
};
|
||||||
|
full = read32(address, offset);
|
||||||
|
split[(field & 0x3)] = (u8)(value >> 8);
|
||||||
|
split[(field & 0x3) + 1] = (u8)(value & 0xff);
|
||||||
|
write32(address, offset, full);
|
||||||
}
|
}
|
||||||
|
|
||||||
void write32(const Device::Address& address, u32 field, u8 value)
|
void write32(const Device::Address& address, u32 field, u32 value)
|
||||||
{
|
{
|
||||||
ignore(address, field, value);
|
IO::outl(PCI_ADDRESS_PORT, make_pci_address(address, field));
|
||||||
todo();
|
IO::outl(PCI_VALUE_PORT, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
780
kernel/src/arch/x86_64/disk/ATA.cpp
Normal file
780
kernel/src/arch/x86_64/disk/ATA.cpp
Normal file
@ -0,0 +1,780 @@
|
|||||||
|
#include "arch/x86_64/disk/ATA.h"
|
||||||
|
#include "Log.h"
|
||||||
|
#include "arch/Serial.h"
|
||||||
|
#include "arch/Timer.h"
|
||||||
|
#include "arch/x86_64/IO.h"
|
||||||
|
#include "memory/MemoryManager.h"
|
||||||
|
#include <luna/Alignment.h>
|
||||||
|
#include <luna/CType.h>
|
||||||
|
#include <luna/SafeArithmetic.h>
|
||||||
|
#include <luna/Vector.h>
|
||||||
|
|
||||||
|
SharedPtr<ATA::Controller> g_controller;
|
||||||
|
|
||||||
|
static void irq_handler(Registers* regs, void* ctx)
|
||||||
|
{
|
||||||
|
((ATA::Channel*)ctx)->irq_handler(regs);
|
||||||
|
}
|
||||||
|
|
||||||
|
static usize copy_ata_string(char* out, u16* in, usize size)
|
||||||
|
{
|
||||||
|
for (usize i = 0; i < size; i += 2)
|
||||||
|
{
|
||||||
|
u16 val = in[i / 2];
|
||||||
|
out[i] = (u8)(val >> 8);
|
||||||
|
out[i + 1] = (u8)(val & 0xff);
|
||||||
|
}
|
||||||
|
|
||||||
|
out[size + 1] = '\0';
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace ATA
|
||||||
|
{
|
||||||
|
Result<void> Controller::scan()
|
||||||
|
{
|
||||||
|
// FIXME: Propagate errors.
|
||||||
|
PCI::scan(
|
||||||
|
[](const PCI::Device& device) {
|
||||||
|
if (!g_controller)
|
||||||
|
{
|
||||||
|
auto controller = adopt_shared_if_nonnull(new (std::nothrow) Controller(device)).release_value();
|
||||||
|
kinfoln("ata: Found ATA controller on PCI bus (%x:%x:%x)", device.address.bus,
|
||||||
|
device.address.function, device.address.slot);
|
||||||
|
|
||||||
|
if (controller->initialize()) g_controller = controller;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ .klass = 1, .subclass = 1 });
|
||||||
|
|
||||||
|
if (!g_controller) kwarnln("ata: No ATA controller found.");
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Controller::initialize()
|
||||||
|
{
|
||||||
|
u16 command_old = PCI::read16(m_device.address, PCI::Command);
|
||||||
|
u16 command_new = command_old;
|
||||||
|
|
||||||
|
command_new &= ~PCI::CMD_INTERRUPT_DISABLE;
|
||||||
|
command_new |= PCI::CMD_IO_SPACE;
|
||||||
|
command_new |= PCI::CMD_BUS_MASTER;
|
||||||
|
|
||||||
|
if (command_new != command_old) PCI::write16(m_device.address, PCI::Command, command_new);
|
||||||
|
|
||||||
|
if (!m_primary_channel.initialize()) return false;
|
||||||
|
|
||||||
|
return m_secondary_channel.initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
Controller::Controller(const PCI::Device& device)
|
||||||
|
: m_device(device), m_primary_channel(this, 0, {}), m_secondary_channel(this, 1, {})
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Channel::Channel(Controller* controller, u8 channel_index, Badge<Controller>)
|
||||||
|
: m_controller(controller), m_channel_index(channel_index)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 Channel::read_register(Register reg)
|
||||||
|
{
|
||||||
|
return IO::inb(m_io_base + (u16)reg);
|
||||||
|
}
|
||||||
|
|
||||||
|
u16 Channel::read_data()
|
||||||
|
{
|
||||||
|
return IO::inw(m_io_base + (u16)Register::Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::write_data(u16 value)
|
||||||
|
{
|
||||||
|
IO::outw(m_io_base + (u16)Register::Data, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::write_register(Register reg, u8 value)
|
||||||
|
{
|
||||||
|
IO::outb(m_io_base + (u16)reg, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 Channel::read_control(ControlRegister reg)
|
||||||
|
{
|
||||||
|
return IO::inb(m_control_base + (u16)reg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::write_control(ControlRegister reg, u8 value)
|
||||||
|
{
|
||||||
|
IO::outb(m_control_base + (u16)reg, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 Channel::read_bm(BusmasterRegister reg)
|
||||||
|
{
|
||||||
|
return IO::inb(m_busmaster_base + (u16)reg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::write_bm(BusmasterRegister reg, u8 value)
|
||||||
|
{
|
||||||
|
IO::outb(m_busmaster_base + (u16)reg, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 Channel::read_prdt_address()
|
||||||
|
{
|
||||||
|
return IO::inl(m_busmaster_base + (u16)BusmasterRegister::PRDTAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::write_prdt_address(u32 value)
|
||||||
|
{
|
||||||
|
IO::outl(m_busmaster_base + (u16)BusmasterRegister::PRDTAddress, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::delay_400ns()
|
||||||
|
{
|
||||||
|
// FIXME: We should use kernel_sleep(), but it doesn't support nanosecond granularity.
|
||||||
|
for (int i = 0; i < 14; i++) { [[maybe_unused]] volatile u8 val = read_control(ControlRegister::AltStatus); }
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::select(u8 drive)
|
||||||
|
{
|
||||||
|
if (drive == m_current_drive) return;
|
||||||
|
|
||||||
|
u8 value = (drive << 4) | 0xa0;
|
||||||
|
write_register(Register::DriveSelect, value);
|
||||||
|
|
||||||
|
delay_400ns();
|
||||||
|
|
||||||
|
m_current_drive = drive;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::irq_handler(Registers*)
|
||||||
|
{
|
||||||
|
// FIXME: Read the Busmaster register to make sure the IRQ is for this channel.
|
||||||
|
|
||||||
|
if (m_current_drive < 2 && m_drives[m_current_drive]) m_drives[m_current_drive]->irq_handler();
|
||||||
|
|
||||||
|
m_irq_called = true;
|
||||||
|
|
||||||
|
if (m_thread) m_thread->wake_up();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::prepare_for_irq()
|
||||||
|
{
|
||||||
|
m_thread = Scheduler::current();
|
||||||
|
m_irq_called = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Channel::wait_for_irq()
|
||||||
|
{
|
||||||
|
if (!m_irq_called) kernel_wait_for_event();
|
||||||
|
|
||||||
|
m_irq_called = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Channel::wait_for_irq_or_timeout(u64 timeout)
|
||||||
|
{
|
||||||
|
if (!m_irq_called)
|
||||||
|
{
|
||||||
|
kernel_sleep(timeout);
|
||||||
|
m_irq_called = false;
|
||||||
|
return m_thread->sleep_ticks_left;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_irq_called = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Channel::wait_for_reg_set(Register reg, u8 value, u64 timeout)
|
||||||
|
{
|
||||||
|
u64 begin = Timer::ticks_ms();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
u8 reg_value = reg == Register::Status ? read_control(ControlRegister::AltStatus) : read_register(reg);
|
||||||
|
if (reg_value & value) return true;
|
||||||
|
if ((Timer::ticks_ms() - begin) >= timeout) return false;
|
||||||
|
kernel_sleep(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Channel::wait_for_reg_clear(Register reg, u8 value, u64 timeout)
|
||||||
|
{
|
||||||
|
u64 begin = Timer::ticks_ms();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
u8 reg_value = reg == Register::Status ? read_control(ControlRegister::AltStatus) : read_register(reg);
|
||||||
|
if ((reg_value & value) == 0) return true;
|
||||||
|
if ((Timer::ticks_ms() - begin) >= timeout) return false;
|
||||||
|
kernel_sleep(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<void> Channel::wait_until_ready()
|
||||||
|
{
|
||||||
|
if (!wait_for_reg_clear(Register::Status, SR_Busy, 1000))
|
||||||
|
{
|
||||||
|
kwarnln("ata: Drive %d:%d timed out (BSY)", m_channel_index, m_current_drive);
|
||||||
|
return err(EIO);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!wait_for_reg_set(Register::Status, SR_DataRequestReady | SR_Error, 1000))
|
||||||
|
{
|
||||||
|
kwarnln("ata: Drive %d:%d timed out (DRQ)", m_channel_index, m_current_drive);
|
||||||
|
return err(EIO);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 status = read_control(ControlRegister::AltStatus);
|
||||||
|
if (status & SR_Error)
|
||||||
|
{
|
||||||
|
kwarnln("ata: An error occurred in drive %d:%d while waiting for data to become available", m_channel_index,
|
||||||
|
m_current_drive);
|
||||||
|
return err(EIO);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Channel::initialize()
|
||||||
|
{
|
||||||
|
int offset = m_channel_index ? 2 : 0;
|
||||||
|
m_is_pci_native_mode = m_controller->device().type.prog_if & (1 << offset);
|
||||||
|
|
||||||
|
u16 control_port_base_address;
|
||||||
|
u16 io_base_address;
|
||||||
|
|
||||||
|
if (m_is_pci_native_mode)
|
||||||
|
{
|
||||||
|
auto io_base = m_controller->device().getBAR(m_channel_index ? 2 : 0);
|
||||||
|
if (!io_base.is_iospace())
|
||||||
|
{
|
||||||
|
kwarnln("ata: Channel %d's IO base BAR is not in IO space", m_channel_index);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
io_base_address = io_base.port();
|
||||||
|
|
||||||
|
auto io_control = m_controller->device().getBAR(m_channel_index ? 3 : 1);
|
||||||
|
if (!io_control.is_iospace())
|
||||||
|
{
|
||||||
|
kwarnln("ata: Channel %d's control base BAR is not in IO space", m_channel_index);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
control_port_base_address = io_control.port() + 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
io_base_address = m_channel_index ? 0x170 : 0x1f0;
|
||||||
|
control_port_base_address = m_channel_index ? 0x376 : 0x3f6;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_io_base = io_base_address;
|
||||||
|
m_control_base = control_port_base_address;
|
||||||
|
|
||||||
|
auto io_busmaster = m_controller->device().getBAR(4);
|
||||||
|
if (!io_busmaster.is_iospace())
|
||||||
|
{
|
||||||
|
kwarnln("ata: Channel %d's busmaster base BAR is not in IO space", m_channel_index);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
m_busmaster_base = io_busmaster.port() + (u16)(m_channel_index * 8u);
|
||||||
|
|
||||||
|
if (m_is_pci_native_mode) m_interrupt_line = PCI::read8(m_controller->device().address, PCI::InterruptLine);
|
||||||
|
else
|
||||||
|
m_interrupt_line = m_channel_index ? 15 : 14;
|
||||||
|
|
||||||
|
write_control(ControlRegister::DeviceControl, 0);
|
||||||
|
|
||||||
|
for (u8 drive = 0; drive < 2; drive++)
|
||||||
|
{
|
||||||
|
ScopedKMutexLock<100> lock(m_lock);
|
||||||
|
|
||||||
|
select(drive);
|
||||||
|
|
||||||
|
if (read_register(Register::Status) == 0)
|
||||||
|
{
|
||||||
|
// No drive on this slot.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
kinfoln("ata: Channel %d has a drive on slot %d!", m_channel_index, drive);
|
||||||
|
|
||||||
|
auto rc = adopt_shared_if_nonnull(new (std::nothrow) Drive(this, drive, {}));
|
||||||
|
if (rc.has_error())
|
||||||
|
{
|
||||||
|
kinfoln("ata: Failed to create drive object: %s", rc.error_string());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_drives[drive] = rc.release_value();
|
||||||
|
|
||||||
|
if (!m_drives[drive]->initialize())
|
||||||
|
{
|
||||||
|
m_drives[drive] = {};
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ok = CPU::register_interrupt(m_interrupt_line, ::irq_handler, this);
|
||||||
|
if (!ok)
|
||||||
|
{
|
||||||
|
kerrorln("ata: Failed to register IRQ handler for ATA channel %d (IRQ %d)", m_channel_index,
|
||||||
|
m_interrupt_line);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (u8 drive = 0; drive < 2; drive++)
|
||||||
|
{
|
||||||
|
if (m_drives[drive])
|
||||||
|
{
|
||||||
|
if (!m_drives[drive]->post_initialize())
|
||||||
|
{
|
||||||
|
m_drives[drive] = {};
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ATADevice::create(m_drives[drive]).has_error())
|
||||||
|
{
|
||||||
|
kwarnln("ata: Failed to register ATA drive %d:%d in DeviceRegistry", m_channel_index, drive);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Drive::Drive(Channel* channel, u8 drive_index, Badge<Channel>) : m_channel(channel), m_drive_index(drive_index)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Drive::identify_ata()
|
||||||
|
{
|
||||||
|
m_channel->write_register(Register::Command, m_is_atapi ? CMD_Identify_Packet : CMD_Identify);
|
||||||
|
|
||||||
|
m_channel->delay_400ns();
|
||||||
|
|
||||||
|
if (!m_channel->wait_for_reg_clear(Register::Status, SR_Busy, 1000))
|
||||||
|
{
|
||||||
|
kwarnln("ata: Drive %d timed out clearing SR_Busy (waited for 1000 ms)", m_drive_index);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_channel->read_register(Register::Status) & SR_Error)
|
||||||
|
{
|
||||||
|
u8 lbam = m_channel->read_register(Register::LBAMiddle);
|
||||||
|
u8 lbah = m_channel->read_register(Register::LBAHigh);
|
||||||
|
|
||||||
|
if ((lbam == 0x14 && lbah == 0xeb) || (lbam == 0x69 && lbah == 0x96))
|
||||||
|
{
|
||||||
|
if (!m_is_atapi)
|
||||||
|
{
|
||||||
|
kinfoln("ata: Drive %d is ATAPI, sending IDENTIFY_PACKET command", m_drive_index);
|
||||||
|
m_is_atapi = true;
|
||||||
|
return identify_ata();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
kwarnln("ata: IDENTIFY command for drive %d returned error", m_drive_index);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!m_channel->wait_for_reg_set(Register::Status, SR_DataRequestReady | SR_Error, 1000))
|
||||||
|
{
|
||||||
|
kwarnln("ata: Drive %d timed out setting SR_DataRequestReady (waited for 1000 ms)", m_drive_index);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 status = m_channel->read_register(Register::Status);
|
||||||
|
if (status & SR_Error)
|
||||||
|
{
|
||||||
|
kwarnln("ata: IDENTIFY command for drive %d returned error", m_drive_index);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (usize i = 0; i < 256; i++)
|
||||||
|
{
|
||||||
|
u16 data = m_channel->read_data();
|
||||||
|
m_identify_words[i] = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Drive::initialize()
|
||||||
|
{
|
||||||
|
m_channel->select(m_drive_index);
|
||||||
|
|
||||||
|
m_channel->write_register(Register::SectorCount, 0);
|
||||||
|
m_channel->write_register(Register::LBALow, 0);
|
||||||
|
m_channel->write_register(Register::LBAMiddle, 0);
|
||||||
|
m_channel->write_register(Register::LBAHigh, 0);
|
||||||
|
|
||||||
|
if (!identify_ata()) return false;
|
||||||
|
|
||||||
|
m_serial.set_length(copy_ata_string(m_serial.data(), &m_identify_words[10], SERIAL_LEN));
|
||||||
|
m_revision.set_length(copy_ata_string(m_revision.data(), &m_identify_words[23], REVISION_LEN));
|
||||||
|
m_model.set_length(copy_ata_string(m_model.data(), &m_identify_words[27], MODEL_LEN));
|
||||||
|
|
||||||
|
m_serial.trim(" ");
|
||||||
|
m_revision.trim(" ");
|
||||||
|
m_model.trim(" ");
|
||||||
|
|
||||||
|
kinfoln("ata: Drive IDENTIFY returned serial='%s', revision='%s' and model='%s'", m_serial.chars(),
|
||||||
|
m_revision.chars(), m_model.chars());
|
||||||
|
|
||||||
|
auto status = m_channel->read_bm(BusmasterRegister::Status);
|
||||||
|
if (status & BMS_SimplexOnly)
|
||||||
|
{
|
||||||
|
kwarnln("ata: Drive %d will not use DMA because of simplex shenanigans", m_drive_index);
|
||||||
|
m_uses_dma = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto frame = MemoryManager::alloc_frame();
|
||||||
|
if (frame.has_error() || frame.value() > 0xffffffff)
|
||||||
|
{
|
||||||
|
kwarnln("ata: Failed to allocate memory below the 32-bit limit for the PRDT");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
m_dma_prdt_phys = frame.release_value();
|
||||||
|
m_dma_prdt = (prdt_entry*)MMU::translate_physical_address(m_dma_prdt_phys);
|
||||||
|
|
||||||
|
memset(m_dma_prdt, 0, ARCH_PAGE_SIZE);
|
||||||
|
|
||||||
|
frame = MemoryManager::alloc_frame();
|
||||||
|
if (frame.has_error() || frame.value() > 0xffffffff)
|
||||||
|
{
|
||||||
|
kwarnln("ata: Failed to allocate memory below the 32-bit limit for DMA memory");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
m_dma_mem_phys = frame.release_value();
|
||||||
|
m_dma_mem = (void*)MMU::translate_physical_address(m_dma_mem_phys);
|
||||||
|
|
||||||
|
memset(const_cast<void*>(m_dma_mem), 0, ARCH_PAGE_SIZE);
|
||||||
|
|
||||||
|
if (m_uses_dma)
|
||||||
|
{
|
||||||
|
auto cmd = m_channel->read_bm(BusmasterRegister::Command);
|
||||||
|
cmd &= ~BMC_StartStop;
|
||||||
|
m_channel->write_bm(BusmasterRegister::Command, cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Drive::post_initialize()
|
||||||
|
{
|
||||||
|
if (m_is_atapi)
|
||||||
|
{
|
||||||
|
atapi_packet packet;
|
||||||
|
memset(&packet, 0, sizeof(packet));
|
||||||
|
packet.command_bytes[0] = ATAPI_ReadCapacity;
|
||||||
|
|
||||||
|
atapi_read_capacity_reply reply;
|
||||||
|
|
||||||
|
if (send_packet_atapi_pio(&packet, &reply, sizeof(reply)).has_error())
|
||||||
|
{
|
||||||
|
kwarnln("ata: Failed to send Read Capacity command to ATAPI drive");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_is_lba48 = true;
|
||||||
|
|
||||||
|
// FIXME: This assumes the host machine is little-endian.
|
||||||
|
u32 last_lba = __builtin_bswap32(reply.last_lba);
|
||||||
|
u32 sector_size = __builtin_bswap32(reply.sector_size);
|
||||||
|
|
||||||
|
m_block_count = last_lba + 1;
|
||||||
|
m_block_size = sector_size;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (m_identify_data.big_lba) m_is_lba48 = true;
|
||||||
|
|
||||||
|
if (m_is_lba48) m_block_count = m_identify_data.sectors_48;
|
||||||
|
else
|
||||||
|
m_block_count = m_identify_data.sectors_28;
|
||||||
|
|
||||||
|
// FIXME: Should we check for CHS?
|
||||||
|
|
||||||
|
// FIXME: Maybe a different block size is in use? Detect that.
|
||||||
|
m_block_size = 512;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 total_capacity;
|
||||||
|
if (!safe_mul(m_block_count, m_block_size).try_set_value(total_capacity))
|
||||||
|
{
|
||||||
|
kwarnln("ata: Drive %d's total capacity is too large", m_drive_index);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
kinfoln("ata: Drive %d capacity information: Block Count=%lu, Block Size=%lu, Total Capacity=%lu",
|
||||||
|
m_drive_index, m_block_count, m_block_size, total_capacity);
|
||||||
|
|
||||||
|
char buf[2048];
|
||||||
|
if (atapi_read_pio(0, buf, sizeof(buf)).has_error()) return false;
|
||||||
|
|
||||||
|
char readable[2049];
|
||||||
|
for (int i = 0; i < 2048; i++) { readable[i] = _iscntrl(buf[i]) ? '.' : buf[i]; }
|
||||||
|
readable[2048] = 0;
|
||||||
|
kinfoln("ata: Read first sector from CD-ROM: %s", readable);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<void> Drive::send_packet_atapi_pio(const atapi_packet* packet, void* out, u16 response_size)
|
||||||
|
{
|
||||||
|
u8* ptr = (u8*)out;
|
||||||
|
|
||||||
|
m_channel->select(m_drive_index);
|
||||||
|
|
||||||
|
// We use PIO here.
|
||||||
|
m_channel->write_register(Register::Features, 0x00);
|
||||||
|
|
||||||
|
m_channel->write_register(Register::LBAMiddle, (u8)(response_size & 0xff));
|
||||||
|
m_channel->write_register(Register::LBAHigh, (u8)(response_size >> 8));
|
||||||
|
|
||||||
|
m_channel->write_register(Register::Command, CMD_Packet);
|
||||||
|
|
||||||
|
m_channel->delay_400ns();
|
||||||
|
|
||||||
|
usize i = 0;
|
||||||
|
|
||||||
|
TRY(m_channel->wait_until_ready());
|
||||||
|
|
||||||
|
for (int j = 0; j < 6; j++) m_channel->write_data(packet->command_words[j]);
|
||||||
|
|
||||||
|
while (i < response_size)
|
||||||
|
{
|
||||||
|
TRY(m_channel->wait_until_ready());
|
||||||
|
|
||||||
|
usize byte_count =
|
||||||
|
m_channel->read_register(Register::LBAHigh) << 8 | m_channel->read_register(Register::LBAMiddle);
|
||||||
|
usize word_count = byte_count / 2;
|
||||||
|
|
||||||
|
while (word_count--)
|
||||||
|
{
|
||||||
|
u16 value = m_channel->read_data();
|
||||||
|
ptr[0] = (u8)(value & 0xff);
|
||||||
|
ptr[1] = (u8)(value >> 8);
|
||||||
|
ptr += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += byte_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
|
||||||
|
Result<void> Drive::send_packet_atapi_dma(const atapi_packet* packet, void* out, u16 response_size)
|
||||||
|
{
|
||||||
|
check(m_uses_dma);
|
||||||
|
|
||||||
|
m_channel->select(m_drive_index);
|
||||||
|
|
||||||
|
kdbgln("have selected");
|
||||||
|
|
||||||
|
// We use DMA here.
|
||||||
|
m_channel->write_register(Register::Features, 0x01);
|
||||||
|
|
||||||
|
m_channel->write_register(Register::LBAMiddle, 0);
|
||||||
|
m_channel->write_register(Register::LBAHigh, 0);
|
||||||
|
|
||||||
|
kdbgln("will do_dma_command");
|
||||||
|
|
||||||
|
TRY(do_dma_command(CMD_Packet, response_size, false));
|
||||||
|
|
||||||
|
TRY(m_channel->wait_until_ready());
|
||||||
|
|
||||||
|
kdbgln("send atapi packet data");
|
||||||
|
|
||||||
|
for (int j = 0; j < 6; j++) m_channel->write_data(packet->command_words[j]);
|
||||||
|
|
||||||
|
kdbgln("do dma transfer");
|
||||||
|
|
||||||
|
TRY(do_dma_transfer());
|
||||||
|
|
||||||
|
memcpy(out, const_cast<void*>(m_dma_mem), response_size);
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<void> Drive::do_dma_command(u8 command, u16 count, bool write)
|
||||||
|
{
|
||||||
|
m_dma_prdt->address = (u32)m_dma_mem_phys;
|
||||||
|
m_dma_prdt->count = count;
|
||||||
|
m_dma_prdt->flags = END_OF_PRDT;
|
||||||
|
|
||||||
|
kdbgln("ata: do_dma_command: phys=%x, command=%x, count=%u, write=%d", m_dma_prdt->address, command, count,
|
||||||
|
write);
|
||||||
|
|
||||||
|
m_channel->write_prdt_address((u32)m_dma_prdt_phys);
|
||||||
|
|
||||||
|
auto status = m_channel->read_bm(BusmasterRegister::Status);
|
||||||
|
status &= ~(BMS_DMAFailure | BMS_IRQPending);
|
||||||
|
m_channel->write_bm(BusmasterRegister::Status, status);
|
||||||
|
|
||||||
|
auto cmd = m_channel->read_bm(BusmasterRegister::Command);
|
||||||
|
if (!write) cmd |= BMC_ReadWrite;
|
||||||
|
else
|
||||||
|
cmd &= ~BMC_ReadWrite;
|
||||||
|
m_channel->write_bm(BusmasterRegister::Command, cmd);
|
||||||
|
|
||||||
|
m_channel->prepare_for_irq();
|
||||||
|
|
||||||
|
m_channel->write_register(Register::Command, command);
|
||||||
|
|
||||||
|
cmd = m_channel->read_bm(BusmasterRegister::Command);
|
||||||
|
cmd |= BMC_StartStop;
|
||||||
|
m_channel->write_bm(BusmasterRegister::Command, cmd);
|
||||||
|
|
||||||
|
m_channel->delay_400ns();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<void> Drive::do_dma_transfer()
|
||||||
|
{
|
||||||
|
if (!m_channel->wait_for_irq_or_timeout(2000))
|
||||||
|
{
|
||||||
|
kwarnln("ata: Drive %d timed out (DMA)", m_drive_index);
|
||||||
|
return err(EIO);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 status = m_channel->read_control(ControlRegister::AltStatus);
|
||||||
|
kdbgln("ata: status after irq: %#x", status);
|
||||||
|
|
||||||
|
m_channel->delay_400ns();
|
||||||
|
|
||||||
|
auto cmd = m_channel->read_bm(BusmasterRegister::Command);
|
||||||
|
cmd &= ~BMC_StartStop;
|
||||||
|
m_channel->write_bm(BusmasterRegister::Command, cmd);
|
||||||
|
|
||||||
|
status = m_channel->read_bm(BusmasterRegister::Status);
|
||||||
|
m_channel->write_bm(BusmasterRegister::Status, status & ~(BMS_DMAFailure | BMS_IRQPending));
|
||||||
|
|
||||||
|
if (status & BMS_DMAFailure)
|
||||||
|
{
|
||||||
|
kwarnln("ata: DMA failure while trying to read drive %d", m_drive_index);
|
||||||
|
return err(EIO);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Result<void> Drive::atapi_read_pio(u64 lba, void* out, usize size)
|
||||||
|
{
|
||||||
|
check(lba < m_block_count);
|
||||||
|
check(size <= ARCH_PAGE_SIZE);
|
||||||
|
|
||||||
|
atapi_packet read_packet;
|
||||||
|
memset(&read_packet, 0, sizeof(read_packet));
|
||||||
|
read_packet.command_bytes[0] = ATAPI_Read;
|
||||||
|
read_packet.command_bytes[2] = (lba >> 0x18) & 0xff;
|
||||||
|
read_packet.command_bytes[3] = (lba >> 0x10) & 0xff;
|
||||||
|
read_packet.command_bytes[4] = (lba >> 0x08) & 0xff;
|
||||||
|
read_packet.command_bytes[5] = (lba >> 0x00) & 0xff;
|
||||||
|
read_packet.command_bytes[9] = (u8)(size / m_block_size);
|
||||||
|
|
||||||
|
return send_packet_atapi_pio(&read_packet, out, (u16)size);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<void> Drive::read_lba(u64 lba, void* out, usize nblocks)
|
||||||
|
{
|
||||||
|
const usize blocks_per_page = ARCH_PAGE_SIZE / m_block_size;
|
||||||
|
if (m_is_atapi)
|
||||||
|
{
|
||||||
|
kdbgln("ata: Reading %zu blocks from ATAPI drive using PIO, at LBA %ld", nblocks, lba);
|
||||||
|
while (nblocks > blocks_per_page)
|
||||||
|
{
|
||||||
|
TRY(atapi_read_pio(lba, out, ARCH_PAGE_SIZE));
|
||||||
|
lba += blocks_per_page;
|
||||||
|
nblocks -= blocks_per_page;
|
||||||
|
out = offset_ptr(out, ARCH_PAGE_SIZE);
|
||||||
|
}
|
||||||
|
return atapi_read_pio(lba, out, nblocks * m_block_size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
todo();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Drive::irq_handler()
|
||||||
|
{
|
||||||
|
// Clear the IRQ flag.
|
||||||
|
u8 status = m_channel->read_register(Register::Status);
|
||||||
|
|
||||||
|
if (status & SR_Error)
|
||||||
|
{
|
||||||
|
u8 error = m_channel->read_register(Register::Error);
|
||||||
|
(void)error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_uses_dma)
|
||||||
|
{
|
||||||
|
status = m_channel->read_bm(BusmasterRegister::Status);
|
||||||
|
if (status & BMS_DMAFailure) { kwarnln("ata: DMA failure in irq"); }
|
||||||
|
m_channel->write_bm(BusmasterRegister::Status, 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static u32 next_minor = 0;
|
||||||
|
|
||||||
|
Result<void> ATADevice::create(SharedPtr<ATA::Drive> drive)
|
||||||
|
{
|
||||||
|
auto device = TRY(adopt_shared_if_nonnull(new (std::nothrow) ATADevice()));
|
||||||
|
device->m_drive = drive;
|
||||||
|
return DeviceRegistry::register_special_device(DeviceRegistry::Disk, next_minor++, device, "cdrom", 0400);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<u64> ATADevice::read(u8* buf, usize offset, usize size) const
|
||||||
|
{
|
||||||
|
if (size == 0) return 0;
|
||||||
|
|
||||||
|
if (offset > m_drive->capacity()) return 0;
|
||||||
|
if (offset + size > m_drive->capacity()) size = m_drive->capacity() - offset;
|
||||||
|
|
||||||
|
usize length = size;
|
||||||
|
|
||||||
|
auto block_size = m_drive->block_size();
|
||||||
|
|
||||||
|
auto* temp = TRY(make_array<u8>(block_size));
|
||||||
|
auto guard = make_scope_guard([temp] { delete[] temp; });
|
||||||
|
|
||||||
|
if (offset % block_size)
|
||||||
|
{
|
||||||
|
usize extra_size = block_size - (offset % block_size);
|
||||||
|
TRY(m_drive->read_lba(offset / block_size, temp, 1));
|
||||||
|
memcpy(buf, temp + (offset % block_size), extra_size);
|
||||||
|
offset += extra_size;
|
||||||
|
size -= extra_size;
|
||||||
|
buf += extra_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (size >= ARCH_PAGE_SIZE)
|
||||||
|
{
|
||||||
|
TRY(m_drive->read_lba(offset / block_size, buf, ARCH_PAGE_SIZE / block_size));
|
||||||
|
offset += ARCH_PAGE_SIZE;
|
||||||
|
size -= ARCH_PAGE_SIZE;
|
||||||
|
buf += ARCH_PAGE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (size >= block_size)
|
||||||
|
{
|
||||||
|
TRY(m_drive->read_lba(offset / block_size, buf, 1));
|
||||||
|
offset += block_size;
|
||||||
|
size -= block_size;
|
||||||
|
buf += block_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
{
|
||||||
|
TRY(m_drive->read_lba(offset / block_size, temp, 1));
|
||||||
|
memcpy(buf, temp, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
309
kernel/src/arch/x86_64/disk/ATA.h
Normal file
309
kernel/src/arch/x86_64/disk/ATA.h
Normal file
@ -0,0 +1,309 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "arch/PCI.h"
|
||||||
|
#include "fs/devices/DeviceRegistry.h"
|
||||||
|
#include "lib/KMutex.h"
|
||||||
|
#include <luna/Atomic.h>
|
||||||
|
#include <luna/SharedPtr.h>
|
||||||
|
#include <luna/StaticString.h>
|
||||||
|
|
||||||
|
namespace ATA
|
||||||
|
{
|
||||||
|
enum class Register : u16
|
||||||
|
{
|
||||||
|
Data = 0,
|
||||||
|
Error = 1,
|
||||||
|
Features = 1,
|
||||||
|
SectorCount = 2,
|
||||||
|
SectorNumber = 3,
|
||||||
|
LBALow = 3,
|
||||||
|
CylinderLow = 4,
|
||||||
|
LBAMiddle = 4,
|
||||||
|
CylinderHigh = 5,
|
||||||
|
LBAHigh = 5,
|
||||||
|
DriveSelect = 6,
|
||||||
|
Status = 7,
|
||||||
|
Command = 7,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class ControlRegister : u16
|
||||||
|
{
|
||||||
|
AltStatus = 0,
|
||||||
|
DeviceControl = 0,
|
||||||
|
DriveAddress = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class BusmasterRegister : u16
|
||||||
|
{
|
||||||
|
Command = 0,
|
||||||
|
Status = 2,
|
||||||
|
PRDTAddress = 4,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum StatusRegister : u8
|
||||||
|
{
|
||||||
|
SR_Busy = 0x80,
|
||||||
|
SR_DriveReady = 0x40,
|
||||||
|
SR_WriteFault = 0x20,
|
||||||
|
SR_SeekComplete = 0x10,
|
||||||
|
SR_DataRequestReady = 0x08,
|
||||||
|
SR_CorrectedData = 0x04,
|
||||||
|
SR_Index = 0x02,
|
||||||
|
SR_Error = 0x01
|
||||||
|
};
|
||||||
|
|
||||||
|
enum CommandRegister : u8
|
||||||
|
{
|
||||||
|
CMD_Identify = 0xec,
|
||||||
|
CMD_Packet = 0xa0,
|
||||||
|
CMD_Identify_Packet = 0xa1
|
||||||
|
};
|
||||||
|
|
||||||
|
enum BusMasterStatus : u8
|
||||||
|
{
|
||||||
|
BMS_SimplexOnly = 0x80,
|
||||||
|
BMS_SlaveInit = 0x40,
|
||||||
|
BMS_MasterInit = 0x20,
|
||||||
|
BMS_IRQPending = 0x4,
|
||||||
|
BMS_DMAFailure = 0x2,
|
||||||
|
BMS_DMAMode = 0x1
|
||||||
|
};
|
||||||
|
|
||||||
|
enum BusMasterCommand : u8
|
||||||
|
{
|
||||||
|
BMC_ReadWrite = 0x8,
|
||||||
|
BMC_StartStop = 0x1,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ATAIdentify
|
||||||
|
{
|
||||||
|
u16 flags;
|
||||||
|
u16 unused1[9];
|
||||||
|
char serial[20];
|
||||||
|
u16 unused2[3];
|
||||||
|
char firmware[8];
|
||||||
|
char model[40];
|
||||||
|
u16 sectors_per_int;
|
||||||
|
u16 unused3;
|
||||||
|
u16 capabilities[2];
|
||||||
|
u16 unused4[2];
|
||||||
|
u16 valid_ext_data;
|
||||||
|
u16 unused5[5];
|
||||||
|
u16 size_of_rw_mult;
|
||||||
|
u32 sectors_28;
|
||||||
|
u16 unused6[21];
|
||||||
|
u16 unused7 : 10;
|
||||||
|
u16 big_lba : 1;
|
||||||
|
u16 unused8 : 5;
|
||||||
|
u16 unused9[17];
|
||||||
|
u64 sectors_48;
|
||||||
|
u16 unused10[152];
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ATAPICommand : u8
|
||||||
|
{
|
||||||
|
ATAPI_ReadCapacity = 0x25,
|
||||||
|
ATAPI_Read = 0xa8,
|
||||||
|
};
|
||||||
|
|
||||||
|
class Controller;
|
||||||
|
class Channel;
|
||||||
|
|
||||||
|
struct prdt_entry
|
||||||
|
{
|
||||||
|
u32 address;
|
||||||
|
u16 count;
|
||||||
|
u16 flags;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct atapi_packet
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
u16 command_words[6];
|
||||||
|
u8 command_bytes[12];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct atapi_read_capacity_reply
|
||||||
|
{
|
||||||
|
u32 last_lba;
|
||||||
|
u32 sector_size;
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr u16 END_OF_PRDT = (1 << 15);
|
||||||
|
|
||||||
|
class Drive
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Drive(Channel* channel, u8 drive_index, Badge<Channel>);
|
||||||
|
|
||||||
|
bool initialize();
|
||||||
|
|
||||||
|
bool post_initialize();
|
||||||
|
|
||||||
|
void irq_handler();
|
||||||
|
|
||||||
|
usize block_size() const
|
||||||
|
{
|
||||||
|
return m_block_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
usize block_count() const
|
||||||
|
{
|
||||||
|
return m_block_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
usize capacity() const
|
||||||
|
{
|
||||||
|
return m_block_count * m_block_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<void> read_lba(u64 lba, void* out, usize nblocks);
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool identify_ata();
|
||||||
|
|
||||||
|
Result<void> send_packet_atapi_pio(const atapi_packet* packet, void* out, u16 response_size);
|
||||||
|
#if 0
|
||||||
|
Result<void> send_packet_atapi_dma(const atapi_packet* packet, void* out, u16 response_size);
|
||||||
|
|
||||||
|
Result<void> do_dma_command(u8 command, u16 count, bool write);
|
||||||
|
Result<void> do_dma_transfer();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Result<void> atapi_read_pio(u64 lba, void* out, usize size);
|
||||||
|
|
||||||
|
Channel* m_channel;
|
||||||
|
|
||||||
|
u8 m_drive_index;
|
||||||
|
union {
|
||||||
|
u16 m_identify_words[256];
|
||||||
|
ATAIdentify m_identify_data;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool m_is_atapi { false };
|
||||||
|
bool m_uses_dma { true };
|
||||||
|
|
||||||
|
bool m_is_lba48;
|
||||||
|
u64 m_block_count;
|
||||||
|
u64 m_block_size;
|
||||||
|
|
||||||
|
prdt_entry* m_dma_prdt;
|
||||||
|
u64 m_dma_prdt_phys;
|
||||||
|
volatile void* m_dma_mem;
|
||||||
|
u64 m_dma_mem_phys;
|
||||||
|
|
||||||
|
constexpr static usize SERIAL_LEN = 20;
|
||||||
|
constexpr static usize REVISION_LEN = 8;
|
||||||
|
constexpr static usize MODEL_LEN = 40;
|
||||||
|
|
||||||
|
StaticString<SERIAL_LEN> m_serial;
|
||||||
|
StaticString<REVISION_LEN> m_revision;
|
||||||
|
StaticString<MODEL_LEN> m_model;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Channel
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Channel(Controller* controller, u8 channel_index, Badge<Controller>);
|
||||||
|
|
||||||
|
u8 read_register(Register reg);
|
||||||
|
u16 read_data();
|
||||||
|
void write_data(u16 value);
|
||||||
|
void write_register(Register reg, u8 value);
|
||||||
|
u8 read_control(ControlRegister reg);
|
||||||
|
void write_control(ControlRegister reg, u8 value);
|
||||||
|
|
||||||
|
u8 read_bm(BusmasterRegister reg);
|
||||||
|
void write_bm(BusmasterRegister reg, u8 value);
|
||||||
|
u32 read_prdt_address();
|
||||||
|
void write_prdt_address(u32 value);
|
||||||
|
|
||||||
|
bool wait_for_reg_set(Register reg, u8 value, u64 timeout);
|
||||||
|
bool wait_for_reg_clear(Register reg, u8 value, u64 timeout);
|
||||||
|
|
||||||
|
Result<void> wait_until_ready();
|
||||||
|
|
||||||
|
void delay_400ns();
|
||||||
|
|
||||||
|
void prepare_for_irq();
|
||||||
|
|
||||||
|
void wait_for_irq();
|
||||||
|
bool wait_for_irq_or_timeout(u64 timeout);
|
||||||
|
void irq_handler(Registers*);
|
||||||
|
|
||||||
|
void select(u8 drive);
|
||||||
|
|
||||||
|
bool initialize();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Controller* m_controller;
|
||||||
|
u8 m_channel_index;
|
||||||
|
bool m_is_pci_native_mode;
|
||||||
|
|
||||||
|
u8 m_interrupt_line;
|
||||||
|
|
||||||
|
KMutex<100> m_lock {};
|
||||||
|
|
||||||
|
Thread* m_thread { nullptr };
|
||||||
|
|
||||||
|
u16 m_io_base;
|
||||||
|
u16 m_control_base;
|
||||||
|
u16 m_busmaster_base;
|
||||||
|
|
||||||
|
bool m_irq_called { false };
|
||||||
|
|
||||||
|
u8 m_current_drive = (u8)-1;
|
||||||
|
|
||||||
|
SharedPtr<Drive> m_drives[2];
|
||||||
|
};
|
||||||
|
|
||||||
|
class Controller
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static Result<void> scan();
|
||||||
|
|
||||||
|
const PCI::Device& device() const
|
||||||
|
{
|
||||||
|
return m_device;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool initialize();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Controller(const PCI::Device& device);
|
||||||
|
PCI::Device m_device;
|
||||||
|
Channel m_primary_channel;
|
||||||
|
Channel m_secondary_channel;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class ATADevice : public Device
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// Initializer for DeviceRegistry.
|
||||||
|
static Result<void> create(SharedPtr<ATA::Drive> drive);
|
||||||
|
|
||||||
|
Result<usize> read(u8*, usize, usize) const override;
|
||||||
|
|
||||||
|
Result<usize> write(const u8*, usize, usize) override
|
||||||
|
{
|
||||||
|
return err(ENOTSUP);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool blocking() const override
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
usize size() const override
|
||||||
|
{
|
||||||
|
return m_drive->capacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~ATADevice() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
ATADevice() = default;
|
||||||
|
SharedPtr<ATA::Drive> m_drive;
|
||||||
|
};
|
@ -84,6 +84,20 @@ INT(20);
|
|||||||
INT(21);
|
INT(21);
|
||||||
INT(32);
|
INT(32);
|
||||||
INT(33);
|
INT(33);
|
||||||
|
INT(34);
|
||||||
|
INT(35);
|
||||||
|
INT(36);
|
||||||
|
INT(37);
|
||||||
|
INT(38);
|
||||||
|
INT(39);
|
||||||
|
INT(40);
|
||||||
|
INT(41);
|
||||||
|
INT(42);
|
||||||
|
INT(43);
|
||||||
|
INT(44);
|
||||||
|
INT(45);
|
||||||
|
INT(46);
|
||||||
|
INT(47);
|
||||||
INT(66);
|
INT(66);
|
||||||
|
|
||||||
void setup_idt()
|
void setup_idt()
|
||||||
@ -112,6 +126,20 @@ void setup_idt()
|
|||||||
TRAP(21);
|
TRAP(21);
|
||||||
IRQ(32);
|
IRQ(32);
|
||||||
IRQ(33);
|
IRQ(33);
|
||||||
|
IRQ(34);
|
||||||
|
IRQ(35);
|
||||||
|
IRQ(36);
|
||||||
|
IRQ(37);
|
||||||
|
IRQ(38);
|
||||||
|
IRQ(39);
|
||||||
|
IRQ(40);
|
||||||
|
IRQ(41);
|
||||||
|
IRQ(42);
|
||||||
|
IRQ(43);
|
||||||
|
IRQ(44);
|
||||||
|
IRQ(45);
|
||||||
|
IRQ(46);
|
||||||
|
IRQ(47);
|
||||||
SYS(66);
|
SYS(66);
|
||||||
|
|
||||||
static IDTR idtr;
|
static IDTR idtr;
|
||||||
|
@ -36,9 +36,18 @@ void remap_pic()
|
|||||||
IO::outb(PIC2_DATA, ICW4_8086);
|
IO::outb(PIC2_DATA, ICW4_8086);
|
||||||
io_delay();
|
io_delay();
|
||||||
|
|
||||||
IO::outb(PIC1_DATA, 0b11111100);
|
IO::outb(PIC1_DATA, 0b11111111);
|
||||||
io_delay();
|
io_delay();
|
||||||
IO::outb(PIC2_DATA, 0b11111111);
|
IO::outb(PIC2_DATA, 0b11111111);
|
||||||
|
io_delay();
|
||||||
|
}
|
||||||
|
|
||||||
|
void change_pic_masks(u8 pic1_mask, u8 pic2_mask)
|
||||||
|
{
|
||||||
|
IO::outb(PIC1_DATA, pic1_mask);
|
||||||
|
io_delay();
|
||||||
|
IO::outb(PIC2_DATA, pic2_mask);
|
||||||
|
io_delay();
|
||||||
}
|
}
|
||||||
|
|
||||||
void pic_eoi(unsigned char irq)
|
void pic_eoi(unsigned char irq)
|
||||||
|
@ -12,6 +12,7 @@ namespace DeviceRegistry
|
|||||||
Console = 1,
|
Console = 1,
|
||||||
Memory = 2,
|
Memory = 2,
|
||||||
Framebuffer = 3,
|
Framebuffer = 3,
|
||||||
|
Disk = 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
Result<SharedPtr<Device>> fetch_special_device(u32 major, u32 minor);
|
Result<SharedPtr<Device>> fetch_special_device(u32 major, u32 minor);
|
||||||
|
77
kernel/src/lib/KMutex.h
Normal file
77
kernel/src/lib/KMutex.h
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Log.h"
|
||||||
|
#include "arch/CPU.h"
|
||||||
|
#include "thread/Scheduler.h"
|
||||||
|
#include "thread/Thread.h"
|
||||||
|
#include <luna/CircularQueue.h>
|
||||||
|
|
||||||
|
template <usize ConcurrentThreads> class KMutex
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void lock()
|
||||||
|
{
|
||||||
|
int expected = 0;
|
||||||
|
while (!m_lock.compare_exchange_strong(expected, 1))
|
||||||
|
{
|
||||||
|
expected = 0;
|
||||||
|
auto* current = Scheduler::current();
|
||||||
|
|
||||||
|
// We cannot be interrupted between these functions, otherwise we might never exit the loop
|
||||||
|
CPU::disable_interrupts();
|
||||||
|
bool ok = m_blocked_threads.try_push(current);
|
||||||
|
if (!ok) kernel_sleep(10);
|
||||||
|
else
|
||||||
|
kernel_wait_for_event();
|
||||||
|
CPU::enable_interrupts();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void unlock()
|
||||||
|
{
|
||||||
|
int expected = 1;
|
||||||
|
if (!m_lock.compare_exchange_strong(expected, 0))
|
||||||
|
{
|
||||||
|
kwarnln("KMutex::unlock() called on an unlocked lock with value %d", expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread* blocked;
|
||||||
|
if (m_blocked_threads.try_pop(blocked)) blocked->wake_up();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool try_lock()
|
||||||
|
{
|
||||||
|
int expected = 0;
|
||||||
|
return m_lock.compare_exchange_strong(expected, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
CircularQueue<Thread*, ConcurrentThreads> m_blocked_threads;
|
||||||
|
Atomic<int> m_lock;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <usize ConcurrentThreads> class ScopedKMutexLock
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ScopedKMutexLock(KMutex<ConcurrentThreads>& lock) : m_lock(lock)
|
||||||
|
{
|
||||||
|
m_lock.lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
~ScopedKMutexLock()
|
||||||
|
{
|
||||||
|
if (!m_taken_over) m_lock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopedKMutexLock(const ScopedKMutexLock&) = delete;
|
||||||
|
ScopedKMutexLock(ScopedKMutexLock&&) = delete;
|
||||||
|
|
||||||
|
KMutex<ConcurrentThreads>& take_over()
|
||||||
|
{
|
||||||
|
m_taken_over = true;
|
||||||
|
return m_lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
KMutex<ConcurrentThreads>& m_lock;
|
||||||
|
bool m_taken_over { false };
|
||||||
|
};
|
@ -1,6 +1,5 @@
|
|||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
#include "arch/CPU.h"
|
#include "arch/CPU.h"
|
||||||
#include "arch/PCI.h"
|
|
||||||
#include "arch/Timer.h"
|
#include "arch/Timer.h"
|
||||||
#include "boot/Init.h"
|
#include "boot/Init.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
@ -11,6 +10,10 @@
|
|||||||
#include "thread/Scheduler.h"
|
#include "thread/Scheduler.h"
|
||||||
#include <luna/Units.h>
|
#include <luna/Units.h>
|
||||||
|
|
||||||
|
#ifdef ARCH_X86_64
|
||||||
|
#include "arch/x86_64/disk/ATA.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
extern void set_host_name(StringView);
|
extern void set_host_name(StringView);
|
||||||
|
|
||||||
void reap_thread()
|
void reap_thread()
|
||||||
@ -53,12 +56,9 @@ Result<void> init()
|
|||||||
auto reap = Scheduler::new_kernel_thread(reap_thread, "[reap]").release_value();
|
auto reap = Scheduler::new_kernel_thread(reap_thread, "[reap]").release_value();
|
||||||
Scheduler::set_reap_thread(reap);
|
Scheduler::set_reap_thread(reap);
|
||||||
|
|
||||||
PCI::scan(
|
#ifdef ARCH_X86_64
|
||||||
[](const PCI::Device& device) {
|
ATA::Controller::scan();
|
||||||
kinfoln("Found PCI mass storage device %.4x:%.4x, at address %u:%u:%u", device.id.vendor, device.id.device,
|
#endif
|
||||||
device.address.bus, device.address.slot, device.address.function);
|
|
||||||
},
|
|
||||||
{ .klass = 1 });
|
|
||||||
|
|
||||||
// Disable console logging before transferring control to userspace.
|
// Disable console logging before transferring control to userspace.
|
||||||
setup_log(log_debug_enabled(), log_serial_enabled(), false);
|
setup_log(log_debug_enabled(), log_serial_enabled(), false);
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
#include "Log.h"
|
|
||||||
#include "fs/VFS.h"
|
|
||||||
#include "memory/MemoryManager.h"
|
|
||||||
#include "sys/Syscall.h"
|
|
||||||
#include "thread/Scheduler.h"
|
|
||||||
#include <bits/makedev.h>
|
|
||||||
#include <luna/PathParser.h>
|
|
||||||
|
|
||||||
Result<u64> sys_mknod(Registers*, SyscallArgs args)
|
|
||||||
{
|
|
||||||
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
|
|
||||||
mode_t mode = (mode_t)args[1];
|
|
||||||
dev_t dev = (dev_t)args[2];
|
|
||||||
|
|
||||||
Thread* current = Scheduler::current();
|
|
||||||
|
|
||||||
u32 maj = luna_dev_major(dev);
|
|
||||||
u32 min = luna_dev_minor(dev);
|
|
||||||
|
|
||||||
auto parser = TRY(PathParser::create(path.chars()));
|
|
||||||
|
|
||||||
auto dirname = TRY(parser.dirname());
|
|
||||||
auto basename = TRY(parser.basename());
|
|
||||||
|
|
||||||
TRY(VFS::validate_filename(basename.view()));
|
|
||||||
|
|
||||||
auto parent = TRY(VFS::resolve_path(dirname.chars(), current->auth, current->current_directory));
|
|
||||||
if (!VFS::can_write(parent, current->auth)) return err(EACCES);
|
|
||||||
|
|
||||||
auto inode = TRY(parent->fs()->create_device_inode(maj, min));
|
|
||||||
TRY(parent->add_entry(inode, basename.chars()));
|
|
||||||
|
|
||||||
TRY(inode->chmod(mode));
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
41
kernel/src/sys/pstat.cpp
Normal file
41
kernel/src/sys/pstat.cpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include "memory/MemoryManager.h"
|
||||||
|
#include "sys/Syscall.h"
|
||||||
|
#include "thread/Scheduler.h"
|
||||||
|
#include <bits/pstat.h>
|
||||||
|
|
||||||
|
static void set_timespec(struct timespec& ts, u64 ticks)
|
||||||
|
{
|
||||||
|
ts.tv_sec = ticks / 1000;
|
||||||
|
ts.tv_nsec = (ticks % 1000) * 1000 * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<u64> sys_pstat(Registers*, SyscallArgs args)
|
||||||
|
{
|
||||||
|
pid_t pid = (pid_t)args[0];
|
||||||
|
struct process* ps = (struct process*)args[1];
|
||||||
|
|
||||||
|
// If pid == -1, return the PID of the last spawned thread.
|
||||||
|
if (pid == -1) return g_threads.expect_last()->id;
|
||||||
|
|
||||||
|
auto* thread = TRY(Result<Thread*>::from_option(Scheduler::find_by_pid(pid), ESRCH));
|
||||||
|
|
||||||
|
struct process proc;
|
||||||
|
proc.ps_pid = (pid_t)thread->id;
|
||||||
|
proc.ps_ppid = thread->parent ? (pid_t)thread->parent->id : 0;
|
||||||
|
proc.ps_uid = thread->auth.uid;
|
||||||
|
proc.ps_gid = thread->auth.gid;
|
||||||
|
proc.ps_euid = thread->auth.euid;
|
||||||
|
proc.ps_egid = thread->auth.egid;
|
||||||
|
proc.ps_state = (int)thread->state;
|
||||||
|
proc.ps_flags = thread->is_kernel ? PS_FLAG_KRNL : 0;
|
||||||
|
set_timespec(proc.ps_time, thread->ticks);
|
||||||
|
set_timespec(proc.ps_ktime, thread->ticks_in_kernel);
|
||||||
|
set_timespec(proc.ps_utime, thread->ticks_in_user);
|
||||||
|
strlcpy(proc.ps_name, thread->name.chars(), sizeof(proc.ps_name));
|
||||||
|
strlcpy(proc.ps_cwd, thread->current_directory_path.is_empty() ? "/" : thread->current_directory_path.chars(),
|
||||||
|
sizeof(proc.ps_cwd));
|
||||||
|
|
||||||
|
if (!MemoryManager::copy_to_user_typed(ps, &proc)) return err(EFAULT);
|
||||||
|
|
||||||
|
return (u64)pid;
|
||||||
|
}
|
@ -25,6 +25,7 @@ set(SOURCES
|
|||||||
src/sys/ioctl.cpp
|
src/sys/ioctl.cpp
|
||||||
src/sys/utsname.cpp
|
src/sys/utsname.cpp
|
||||||
src/sys/mount.cpp
|
src/sys/mount.cpp
|
||||||
|
src/sys/pstat.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
if(${LUNA_ARCH} STREQUAL "x86_64")
|
if(${LUNA_ARCH} STREQUAL "x86_64")
|
||||||
|
34
libc/include/bits/pstat.h
Normal file
34
libc/include/bits/pstat.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* bits/pstat.h: The process structure and flags for the pstat() system call. */
|
||||||
|
|
||||||
|
#ifndef _BITS_PSTAT_H
|
||||||
|
#define _BITS_PSTAT_H
|
||||||
|
|
||||||
|
#include <bits/timespec.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#define PS_IDLE 1
|
||||||
|
#define PS_RUNNABLE 2
|
||||||
|
#define PS_SLEEPING 3
|
||||||
|
#define PS_WAITING 4
|
||||||
|
#define PS_ENDED 5
|
||||||
|
|
||||||
|
#define PS_FLAG_KRNL 1
|
||||||
|
|
||||||
|
struct process
|
||||||
|
{
|
||||||
|
pid_t ps_pid;
|
||||||
|
pid_t ps_ppid;
|
||||||
|
uid_t ps_uid;
|
||||||
|
uid_t ps_euid;
|
||||||
|
gid_t ps_gid;
|
||||||
|
gid_t ps_egid;
|
||||||
|
int ps_state;
|
||||||
|
int ps_flags;
|
||||||
|
struct timespec ps_time;
|
||||||
|
struct timespec ps_ktime;
|
||||||
|
struct timespec ps_utime;
|
||||||
|
char ps_name[129];
|
||||||
|
char ps_cwd[256];
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
23
libc/include/sys/pstat.h
Normal file
23
libc/include/sys/pstat.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* sys/pstat.h: The pstat() system call. */
|
||||||
|
|
||||||
|
#ifndef _SYS_PSTAT_H
|
||||||
|
#define _SYS_PSTAT_H
|
||||||
|
|
||||||
|
#include <bits/pstat.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Retrieve information about a process. */
|
||||||
|
pid_t pstat(pid_t pid, struct process* ps);
|
||||||
|
|
||||||
|
/* Get a string representing a process' state. */
|
||||||
|
const char* ps_state_name(int state);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
@ -15,9 +15,6 @@ extern "C"
|
|||||||
/* Create a directory. */
|
/* Create a directory. */
|
||||||
int mkdir(const char* path, mode_t mode);
|
int mkdir(const char* path, mode_t mode);
|
||||||
|
|
||||||
/* Create a special file. */
|
|
||||||
int mknod(const char* path, mode_t mode, dev_t dev);
|
|
||||||
|
|
||||||
/* Change the mode bits of a file. */
|
/* Change the mode bits of a file. */
|
||||||
int chmod(const char* path, mode_t mode);
|
int chmod(const char* path, mode_t mode);
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <grp.h>
|
#include <grp.h>
|
||||||
#include <luna/Vector.h>
|
#include <luna/Vector.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -20,6 +21,7 @@ extern "C"
|
|||||||
{
|
{
|
||||||
f = fopen("/etc/group", "r");
|
f = fopen("/etc/group", "r");
|
||||||
if (!f) return nullptr;
|
if (!f) return nullptr;
|
||||||
|
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
@ -82,14 +84,9 @@ extern "C"
|
|||||||
|
|
||||||
while ((entry = getgrent()))
|
while ((entry = getgrent()))
|
||||||
{
|
{
|
||||||
if (!strcmp(entry->gr_name, name))
|
if (!strcmp(entry->gr_name, name)) { return entry; }
|
||||||
{
|
|
||||||
endgrent();
|
|
||||||
return entry;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
endgrent();
|
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,14 +98,9 @@ extern "C"
|
|||||||
|
|
||||||
while ((entry = getgrent()))
|
while ((entry = getgrent()))
|
||||||
{
|
{
|
||||||
if (entry->gr_gid == gid)
|
if (entry->gr_gid == gid) { return entry; }
|
||||||
{
|
|
||||||
endgrent();
|
|
||||||
return entry;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
endgrent();
|
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -45,6 +46,7 @@ extern "C"
|
|||||||
{
|
{
|
||||||
f = fopen("/etc/passwd", "r");
|
f = fopen("/etc/passwd", "r");
|
||||||
if (!f) return nullptr;
|
if (!f) return nullptr;
|
||||||
|
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
@ -93,14 +95,9 @@ extern "C"
|
|||||||
|
|
||||||
while ((entry = getpwent()))
|
while ((entry = getpwent()))
|
||||||
{
|
{
|
||||||
if (!strcmp(entry->pw_name, name))
|
if (!strcmp(entry->pw_name, name)) { return entry; }
|
||||||
{
|
|
||||||
endpwent();
|
|
||||||
return entry;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
endpwent();
|
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,14 +109,9 @@ extern "C"
|
|||||||
|
|
||||||
while ((entry = getpwent()))
|
while ((entry = getpwent()))
|
||||||
{
|
{
|
||||||
if (entry->pw_uid == uid)
|
if (entry->pw_uid == uid) { return entry; }
|
||||||
{
|
|
||||||
endpwent();
|
|
||||||
return entry;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
endpwent();
|
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
libc/src/sys/pstat.cpp
Normal file
26
libc/src/sys/pstat.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <bits/errno-return.h>
|
||||||
|
#include <sys/pstat.h>
|
||||||
|
#include <sys/syscall.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
pid_t pstat(pid_t pid, struct process* ps)
|
||||||
|
{
|
||||||
|
long rc = syscall(SYS_pstat, pid, ps);
|
||||||
|
__errno_return(rc, pid_t);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* ps_state_name(int state)
|
||||||
|
{
|
||||||
|
switch (state)
|
||||||
|
{
|
||||||
|
case PS_IDLE: return "Idle";
|
||||||
|
case PS_RUNNABLE: return "Running";
|
||||||
|
case PS_SLEEPING: return "Sleeping";
|
||||||
|
case PS_WAITING: return "Waiting";
|
||||||
|
case PS_ENDED: return "Zombie";
|
||||||
|
default: return "???";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,12 +12,6 @@ extern "C"
|
|||||||
__errno_return(rc, int);
|
__errno_return(rc, int);
|
||||||
}
|
}
|
||||||
|
|
||||||
int mknod(const char* path, mode_t mode, dev_t dev)
|
|
||||||
{
|
|
||||||
long rc = syscall(SYS_mknod, path, mode, dev);
|
|
||||||
__errno_return(rc, int);
|
|
||||||
}
|
|
||||||
|
|
||||||
int chmod(const char* path, mode_t mode)
|
int chmod(const char* path, mode_t mode)
|
||||||
{
|
{
|
||||||
long rc = syscall(SYS_fchmodat, AT_FDCWD, path, mode, 0);
|
long rc = syscall(SYS_fchmodat, AT_FDCWD, path, mode, 0);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <luna/CString.h>
|
#include <luna/CString.h>
|
||||||
|
#include <luna/StringView.h>
|
||||||
#include <luna/Types.h>
|
#include <luna/Types.h>
|
||||||
|
|
||||||
template <usize Size> class StaticString
|
template <usize Size> class StaticString
|
||||||
@ -24,12 +25,27 @@ template <usize Size> class StaticString
|
|||||||
m_length = length;
|
m_length = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void adopt(StringView string)
|
||||||
|
{
|
||||||
|
usize length = strlcpy(m_buffer, string.chars(),
|
||||||
|
string.length() > sizeof(m_buffer) ? sizeof(m_buffer) : string.length() + 1);
|
||||||
|
if (length > Size) { m_length = Size; }
|
||||||
|
else
|
||||||
|
m_length = length;
|
||||||
|
}
|
||||||
|
|
||||||
StaticString<Size>& operator=(const char* string)
|
StaticString<Size>& operator=(const char* string)
|
||||||
{
|
{
|
||||||
adopt(string);
|
adopt(string);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StaticString<Size>& operator=(StringView string)
|
||||||
|
{
|
||||||
|
adopt(string);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
template <usize OtherSize> StaticString<Size>& operator=(const StaticString<OtherSize>& string)
|
template <usize OtherSize> StaticString<Size>& operator=(const StaticString<OtherSize>& string)
|
||||||
{
|
{
|
||||||
if constexpr (OtherSize == Size)
|
if constexpr (OtherSize == Size)
|
||||||
@ -46,11 +62,38 @@ template <usize Size> class StaticString
|
|||||||
return m_buffer;
|
return m_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* data()
|
||||||
|
{
|
||||||
|
return m_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_length(usize len)
|
||||||
|
{
|
||||||
|
m_length = len;
|
||||||
|
}
|
||||||
|
|
||||||
usize length() const
|
usize length() const
|
||||||
{
|
{
|
||||||
return m_length;
|
return m_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void trim(StringView delim)
|
||||||
|
{
|
||||||
|
isize i = (isize)m_length;
|
||||||
|
|
||||||
|
while (i--)
|
||||||
|
{
|
||||||
|
char c = m_buffer[i];
|
||||||
|
if (!strchr(delim.chars(), c)) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
|
||||||
|
m_buffer[i] = '\0';
|
||||||
|
|
||||||
|
m_length = (usize)i;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char m_buffer[Size + 1];
|
char m_buffer[Size + 1];
|
||||||
usize m_length { 0 };
|
usize m_length { 0 };
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
#define enumerate_syscalls(_e) \
|
#define enumerate_syscalls(_e) \
|
||||||
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(openat) _e(close) _e(read) _e(getpid) _e(write) \
|
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(openat) _e(close) _e(read) _e(getpid) _e(write) \
|
||||||
_e(lseek) _e(mkdir) _e(execve) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) \
|
_e(lseek) _e(mkdir) _e(execve) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) _e(geteuid) \
|
||||||
_e(geteuid) _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) \
|
_e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) _e(ioctl) \
|
||||||
_e(ioctl) _e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) \
|
_e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
|
||||||
_e(mount) _e(umount)
|
_e(umount) _e(pstat)
|
||||||
|
|
||||||
enum Syscalls
|
enum Syscalls
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user