#define SET_PDE(ptd, pde_index, pde) *((ptd_t*)ptd + pde_index) = pde;
#define SET_PTE(ptd, pt_index, pte_index, pte) \
*(PT_ADDR(ptd, pt_index) + pte_index) = pte;
-#define sym_val(sym) (uintptr_t)(&sym)
+#define sym_val(sym) (ptr_t)(&sym)
#define KERNEL_PAGE_COUNT \
((sym_val(__kernel_end) - sym_val(__kernel_start) + 0x1000 - 1) >> 12);
#define PG_TABLE_STACK 8
// Provided by linker (see linker.ld)
-extern uint8_t __kernel_start;
-extern uint8_t __kernel_end;
-extern uint8_t __init_hhk_end;
-extern uint8_t _k_stack;
+extern u8_t __kernel_start;
+extern u8_t __kernel_end;
+extern u8_t __init_hhk_end;
+extern u8_t _k_stack;
void
_init_page(ptd_t* ptd)
}
// 计算内核.text段的物理地址
- uintptr_t kernel_pm = V2P(&__kernel_start);
+ ptr_t kernel_pm = V2P(&__kernel_start);
// 重映射内核至高半区地址(>=0xC0000000)
for (u32_t i = 0; i < kernel_pg_counts; i++) {
}
u32_t
-__save_subset(uint8_t* destination, uint8_t* base, unsigned int size)
+__save_subset(u8_t* destination, u8_t* base, unsigned int size)
{
unsigned int i = 0;
for (; i < size; i++) {
}
void
-_save_multiboot_info(multiboot_info_t* info, uint8_t* destination)
+_save_multiboot_info(multiboot_info_t* info, u8_t* destination)
{
u32_t current = 0;
- uint8_t* info_b = (uint8_t*)info;
+ u8_t* info_b = (u8_t*)info;
for (; current < sizeof(multiboot_info_t); current++) {
*(destination + current) = *(info_b + current);
}
- ((multiboot_info_t*)destination)->mmap_addr =
- (uintptr_t)destination + current;
+ ((multiboot_info_t*)destination)->mmap_addr = (ptr_t)destination + current;
current += __save_subset(
- destination + current, (uint8_t*)info->mmap_addr, info->mmap_length);
+ destination + current, (u8_t*)info->mmap_addr, info->mmap_length);
if (present(info->flags, MULTIBOOT_INFO_DRIVE_INFO)) {
((multiboot_info_t*)destination)->drives_addr =
- (uintptr_t)destination + current;
- current += __save_subset(destination + current,
- (uint8_t*)info->drives_addr,
- info->drives_length);
+ (ptr_t)destination + current;
+ current += __save_subset(
+ destination + current, (u8_t*)info->drives_addr, info->drives_length);
}
}
// 初始化 kpg 全为0
// P.s. 真没想到GRUB会在这里留下一堆垃圾! 老子的页表全乱套了!
- uint8_t* kpg = (uint8_t*)ptd;
+ u8_t* kpg = (u8_t*)ptd;
for (u32_t i = 0; i < kpg_size; i++) {
*(kpg + i) = 0;
}
O := -O2
W := -Wall -Wextra -Wno-unknown-pragmas \
-Wno-unused-function \
+ -Wno-unused-variable\
-Wno-unused-but-set-variable \
-Wno-unused-parameter \
- -Wno-unused-variable\
+ -Wno-discarded-qualifiers\
-Werror=incompatible-pointer-types
OFLAGS := -fno-gcse\
LOG_MODULE("DBG");
-static volatile sdbg_state = 0;
+static volatile int sdbg_state = 0;
void
sdbg_printf(char* fmt, ...)
void
lunaix_sdbg_loop(isr_param* param)
{
- char c;
+ u8_t c;
if (sdbg_state == SDBG_STATE_WAIT_BRK) {
(param)->execp->eflags &= ~(1 << 8);
}
while (1) {
- c = serial_rx_byte(SERIAL_COM1);
+ c = (u8_t)serial_rx_byte(SERIAL_COM1);
if (c == SDBG_CLNT_QUIT) {
sdbg_state = SDBG_STATE_START;
break;
return;
case SDBG_CLNT_BRKP:
// the break point address
- // serial_rx_buffer(SERIAL_COM1, buffer, sizeof(uintptr_t));
+ // serial_rx_buffer(SERIAL_COM1, buffer, sizeof(ptr_t));
- // asm("movl %0, %%dr0" ::"r"(*((uintptr_t*)buffer)));
+ // asm("movl %0, %%dr0" ::"r"(*((ptr_t*)buffer)));
sdbg_state = SDBG_STATE_WAIT_BRK;
return;
****************************************************************************/
#ifndef GDBSTUB_DONT_DEFINE_STDINT_TYPES
-typedef unsigned char uint8_t;
-typedef unsigned short uint16_t;
+typedef unsigned char u8_t;
+typedef unsigned short u16_t;
typedef unsigned long uint32_t;
#endif
struct gdb_idtr
{
- uint16_t len;
+ u16_t len;
uint32_t offset;
} __attribute__((packed));
struct gdb_idt_gate
{
- uint16_t offset_low;
- uint16_t segment;
- uint16_t flags;
- uint16_t offset_high;
+ u16_t offset_low;
+ u16_t segment;
+ u16_t flags;
+ u16_t offset_high;
} __attribute__((packed));
/*****************************************************************************
}
kprintf(KINFO "ACPI: %s\n", ctx->oem_id);
+
+ return 0;
}
acpi_context*
int
acpi_rsdp_validate(acpi_rsdp_t* rsdp)
{
- uint8_t sum = 0;
- uint8_t* rsdp_ptr = (uint8_t*)rsdp;
+ u8_t sum = 0;
+ u8_t* rsdp_ptr = (u8_t*)rsdp;
for (size_t i = 0; i < 20; i++) {
sum += *(rsdp_ptr + i);
}
return sum == 0;
}
-uint8_t
-acpi_gistranslate(uint8_t old_irq)
+u8_t
+acpi_gistranslate(u8_t old_irq)
{
if (old_irq >= 24) {
return old_irq;
}
acpi_intso_t* int_override = ctx->madt.irq_exception[old_irq];
- return int_override ? (uint8_t)int_override->gsi : old_irq;
+ return int_override ? (u8_t)int_override->gsi : old_irq;
}
#define VIRTUAL_BOX_PROBLEM
continue;
}
- uint8_t* mem_start = entry.addr_low & ~0xf;
+ u8_t* mem_start = entry.addr_low & ~0xf;
size_t len = entry.len_low;
for (size_t j = 0; j < len; j += 16) {
u32_t sig_low = *((u32_t*)(mem_start + j));
}
#else
// You know what, I just search the entire 1MiB for Celestia's sake.
- uint8_t* mem_start = 0x4000;
+ ptr_t mem_start = 0x4000;
for (; mem_start < 0x100000; mem_start += 16) {
- u32_t sig_low = *((u32_t*)(mem_start));
+ u32_t sig_low = *((u32_t*)mem_start);
// XXX: do we need to compare this as well?
// u32_t sig_high = *((u32_t*)(mem_start+j) + 1);
+
if (sig_low == ACPI_RSDP_SIG_L) {
- rsdp = (acpi_rsdp_t*)(mem_start);
+ rsdp = (acpi_rsdp_t*)mem_start;
break;
}
}
toc->madt.apic_addr = madt->apic_addr;
// FUTURE: make madt.{apic,ioapic} as array or linked list.
- uint8_t* ics_start = (uint8_t*)((uintptr_t)madt + sizeof(acpi_madt_t));
- uintptr_t ics_end = (uintptr_t)madt + madt->header.length;
+ ptr_t ics_start = (ptr_t)madt + sizeof(acpi_madt_t);
+ ptr_t ics_end = (ptr_t)madt + madt->header.length;
// Cosidering only one IOAPIC present (max 24 pins)
toc->madt.irq_exception =
size_t alloc_num = (mcfg->length - sizeof(acpi_sdthdr_t) - 8) /
sizeof(struct acpi_mcfg_alloc);
struct acpi_mcfg_alloc* allocs =
- (struct acpi_mcfg_alloc*)((uintptr_t)mcfg + (sizeof(acpi_sdthdr_t) + 8));
+ (struct acpi_mcfg_alloc*)((ptr_t)mcfg + (sizeof(acpi_sdthdr_t) + 8));
toc->mcfg.alloc_num = alloc_num;
toc->mcfg.allocations = valloc(sizeof(struct mcfg_alloc_info) * alloc_num);
}
// 如果port未响应,则继续执行重置
port_reg[HBA_RPxSCTL] = (port_reg[HBA_RPxSCTL] & ~0xf) | 1;
- io_delay(100000); //等待至少一毫秒,差不多就行了
+ io_delay(100000); // 等待至少一毫秒,差不多就行了
port_reg[HBA_RPxSCTL] &= ~0xf;
}
hba->ports_bmp = pmap;
/* ------ HBA端口配置 ------ */
- uintptr_t clb_pg_addr, fis_pg_addr, clb_pa, fis_pa;
+ ptr_t clb_pg_addr = 0, fis_pg_addr = 0;
+ ptr_t clb_pa = 0, fis_pa = 0;
+
for (size_t i = 0, fisp = 0, clbp = 0; i < 32;
i++, pmap >>= 1, fisp = (fisp + 1) % 16, clbp = (clbp + 1) % 4) {
if (!(pmap & 0x1)) {
if (!clbp) {
// 每页最多4个命令队列
clb_pa = pmm_alloc_page(KERNEL_PID, PP_FGLOCKED);
- clb_pg_addr = ioremap(clb_pa, 0x1000);
- memset(clb_pg_addr, 0, 0x1000);
+ clb_pg_addr = (ptr_t)ioremap(clb_pa, 0x1000);
+ memset((void*)clb_pg_addr, 0, 0x1000);
}
if (!fisp) {
// 每页最多16个FIS
fis_pa = pmm_alloc_page(KERNEL_PID, PP_FGLOCKED);
- fis_pg_addr = ioremap(fis_pa, 0x1000);
- memset(fis_pg_addr, 0, 0x1000);
+ fis_pg_addr = (ptr_t)ioremap(fis_pa, 0x1000);
+ memset((void*)fis_pg_addr, 0, 0x1000);
}
/* 重定向CLB与FIS */
port_regs[HBA_RPxCLB] = clb_pa + clbp * HBA_CLB_SIZE;
port_regs[HBA_RPxFB] = fis_pa + fisp * HBA_FIS_SIZE;
- *port = (struct hba_port){ .regs = port_regs,
- .ssts = port_regs[HBA_RPxSSTS],
- .cmdlst = clb_pg_addr + clbp * HBA_CLB_SIZE,
- .fis = fis_pg_addr + fisp * HBA_FIS_SIZE,
- .hba = hba };
+ *port = (struct hba_port){
+ .regs = port_regs,
+ .ssts = port_regs[HBA_RPxSSTS],
+ .cmdlst = (struct hba_cmdh*)(clb_pg_addr + clbp * HBA_CLB_SIZE),
+ .fis = (void*)(fis_pg_addr + fisp * HBA_FIS_SIZE),
+ .hba = hba
+ };
/* 初始化端口,并置于就绪状态 */
port_regs[HBA_RPxCI] = 0;
void
sata_create_fis(struct sata_reg_fis* cmd_fis,
- uint8_t command,
- uint64_t lba,
- uint16_t sector_count)
+ u8_t command,
+ lba_t lba,
+ u16_t sector_count)
{
cmd_fis->head.type = SATA_REG_FIS_H2D;
cmd_fis->head.options = SATA_REG_FIS_COMMAND;
int
hba_bind_sbuf(struct hba_cmdh* cmdh, struct hba_cmdt* cmdt, struct membuf mbuf)
{
- assert_msg(mbuf.size <= 0x400000, "HBA: Buffer too big");
+ assert_msg(mbuf.size <= 0x400000U, "HBA: Buffer too big");
cmdh->prdt_len = 1;
- cmdt->entries[0] = (struct hba_prdte){ .data_base = vmm_v2p(mbuf.buffer),
- .byte_count = mbuf.size - 1 };
+ cmdt->entries[0] =
+ (struct hba_prdte){ .data_base = vmm_v2p((ptr_t)mbuf.buffer),
+ .byte_count = mbuf.size - 1 };
+
+ return 0;
}
int
do {
assert_msg(i < HBA_MAX_PRDTE, "HBA: Too many PRDTEs");
- assert_msg(pos->buf.size <= 0x400000, "HBA: Buffer too big");
+ assert_msg(pos->buf.size <= 0x400000U, "HBA: Buffer too big");
cmdt->entries[i++] =
- (struct hba_prdte){ .data_base = vmm_v2p(pos->buf.buffer),
+ (struct hba_prdte){ .data_base = vmm_v2p((ptr_t)pos->buf.buffer),
.byte_count = pos->buf.size - 1 };
pos = list_entry(pos->components.next, struct vecbuf, components);
} while (pos != vbuf);
cmdh->prdt_len = i + 1;
+
+ return 0;
}
int
memset(cmd_header, 0, sizeof(*cmd_header));
// 将命令表挂到命令头上
- cmd_header->cmd_table_base = vmm_v2p(cmd_table);
+ cmd_header->cmd_table_base = vmm_v2p((ptr_t)cmd_table);
cmd_header->options =
HBA_CMDH_FIS_LEN(sizeof(struct sata_reg_fis)) | HBA_CMDH_CLR_BUSY;
port->regs[HBA_RPxIE] &= ~HBA_MY_IE;
// 预备DMA接收缓存,用于存放HBA传回的数据
- uint16_t* data_in = (uint16_t*)valloc_dma(512);
+ u16_t* data_in = (u16_t*)valloc_dma(512);
int slot = hba_prepare_cmd(port, &cmd_table, &cmd_header);
hba_bind_sbuf(
header->options |= HBA_CMDH_WRITE * write;
- uint16_t count = ICEIL(vbuf_size(io_req->vbuf), port->device->block_size);
+ u16_t count = ICEIL(vbuf_size(io_req->vbuf), port->device->block_size);
struct sata_reg_fis* fis = (struct sata_reg_fis*)table->command_fis;
if ((port->device->flags & HBA_DEV_FEXTLBA)) {
void
scsi_create_packet12(struct scsi_cdb12* cdb,
- uint8_t opcode,
+ u8_t opcode,
u32_t lba,
u32_t alloc_size)
{
void
scsi_create_packet16(struct scsi_cdb16* cdb,
- uint8_t opcode,
- uint64_t lba,
+ u8_t opcode,
+ lba_t lba,
u32_t alloc_size)
{
memset(cdb, 0, sizeof(*cdb));
scsi_parse_capacity(struct hba_device* device, u32_t* parameter)
{
if (device->cbd_size == SCSI_CDB16) {
- device->max_lba =
- SCSI_FLIP(*(parameter + 1)) | (SCSI_FLIP(*parameter) << 32);
+ device->max_lba = (lba_t)SCSI_FLIP(*(parameter + 1)) |
+ ((lba_t)SCSI_FLIP(*parameter) << 32);
device->block_size = SCSI_FLIP(*(parameter + 2));
} else {
// for READ_CAPACITY(10)
}
// field: cdb->misc1
- *((uint8_t*)cdb + 1) = 3 << 5; // RPROTECT=011b 禁用保护检查
+ *((u8_t*)cdb + 1) = 3 << 5; // RPROTECT=011b 禁用保护检查
// The async way...
struct hba_cmd_state* cmds = valloc(sizeof(struct hba_cmd_state));
struct ahci_driver *pos, *n;
llist_for_each(pos, n, &ahcis, ahci_drvs)
{
- if (pos->id == param->execp->vector) {
+ if (pos->id == (int)param->execp->vector) {
hba = &pos->hba;
goto proceed;
}
static u32_t cdb_size[] = { SCSI_CDB12, SCSI_CDB16, 0, 0 };
void
-ahci_parse_dev_info(struct hba_device* dev_info, uint16_t* data)
+ahci_parse_dev_info(struct hba_device* dev_info, u16_t* data)
{
dev_info->max_lba = *((u32_t*)(data + IDDEV_OFFMAXLBA));
dev_info->block_size = *((u32_t*)(data + IDDEV_OFFLSECSIZE));
dev_info->cbd_size = cdb_size[(*data & 0x3)];
- dev_info->wwn = *(uint64_t*)(data + IDDEV_OFFWWN);
+ dev_info->wwn = *(u64_t*)(data + IDDEV_OFFWWN);
dev_info->block_per_sec = 1 << (*(data + IDDEV_OFFLPP) & 0xf);
dev_info->alignment_offset = *(data + IDDEV_OFFALIGN) & 0x3fff;
dev_info->capabilities = *((u32_t*)(data + IDDEV_OFFCAPABILITIES));
if ((*(data + IDDEV_OFFADDSUPPORT) & 0x8) &&
(*(data + IDDEV_OFFA48SUPPORT) & 0x400)) {
- dev_info->max_lba = *((uint64_t*)(data + IDDEV_OFFMAXLBA_EXT));
+ dev_info->max_lba = *((lba_t*)(data + IDDEV_OFFMAXLBA_EXT));
dev_info->flags |= HBA_DEV_FEXTLBA;
}
}
void
-ahci_parsestr(char* str, uint16_t* reg_start, int size_word)
+ahci_parsestr(char* str, u16_t* reg_start, int size_word)
{
int j = 0;
for (int i = 0; i < size_word; i++, j += 2) {
- uint16_t reg = *(reg_start + i);
+ u16_t reg = *(reg_start + i);
str[j] = (char)(reg >> 8);
str[j + 1] = (char)(reg & 0xff);
}
// As we are going to use APIC, disable the old 8259 PIC
pic_disable();
- _apic_base = ioremap(__APIC_BASE_PADDR, 4096);
+ _apic_base = (ptr_t)ioremap(__APIC_BASE_PADDR, 4096);
// Hardware enable the APIC
// By setting bit 11 of IA32_APIC_BASE register
int
rnd_is_supported()
{
- reg32 eax, ebx, ecx, edx;
+ reg32 eax = 0, ebx = 0, ecx = 0, edx = 0;
__get_cpuid(0x01, &eax, &ebx, &ecx, &edx);
return (ecx & (1 << 30));
}
\ No newline at end of file
#define IOAPIC_REG_SEL *((volatile u32_t*)(_ioapic_base + IOAPIC_IOREGSEL))
#define IOAPIC_REG_WIN *((volatile u32_t*)(_ioapic_base + IOAPIC_IOWIN))
-static volatile uintptr_t _ioapic_base;
+static volatile ptr_t _ioapic_base;
void
ioapic_init()
acpi_context* acpi_ctx = acpi_get_context();
- _ioapic_base = ioremap(acpi_ctx->madt.ioapic->ioapic_addr & ~0xfff, 4096);
+ _ioapic_base =
+ (ptr_t)ioremap(acpi_ctx->madt.ioapic->ioapic_addr & ~0xfff, 4096);
}
void
-ioapic_write(uint8_t sel, u32_t val)
+ioapic_write(u8_t sel, u32_t val)
{
IOAPIC_REG_SEL = sel;
IOAPIC_REG_WIN = val;
}
u32_t
-ioapic_read(uint8_t sel)
+ioapic_read(u8_t sel)
{
IOAPIC_REG_SEL = sel;
return IOAPIC_REG_WIN;
}
void
-ioapic_redirect(uint8_t irq, uint8_t vector, uint8_t dest, u32_t flags)
+ioapic_redirect(u8_t irq, u8_t vector, u8_t dest, u32_t flags)
{
- uint8_t reg_sel = IOAPIC_IOREDTBL_BASE + irq * 2;
+ u8_t reg_sel = IOAPIC_IOREDTBL_BASE + irq * 2;
// Write low 32 bits
ioapic_write(reg_sel, (vector | flags) & 0x1FFFF);
}
struct pci_device*
-pci_get_device_by_id(uint16_t vendorId, uint16_t deviceId)
+pci_get_device_by_id(u16_t vendorId, u16_t deviceId)
{
u32_t dev_info = vendorId | (deviceId << 16);
struct pci_device *pos, *n;
void
rtc_init() {
- uint8_t regA = rtc_read_reg(RTC_REG_A | WITH_NMI_DISABLED);
+ u8_t regA = rtc_read_reg(RTC_REG_A | WITH_NMI_DISABLED);
regA = (regA & ~0x7f) | RTC_FREQUENCY_1024HZ | RTC_DIVIDER_33KHZ;
rtc_write_reg(RTC_REG_A | WITH_NMI_DISABLED, regA);
rtc_disable_timer();
}
-uint8_t
-rtc_read_reg(uint8_t reg_selector)
+u8_t
+rtc_read_reg(u8_t reg_selector)
{
io_outb(RTC_INDEX_PORT, reg_selector);
return io_inb(RTC_TARGET_PORT);
}
void
-rtc_write_reg(uint8_t reg_selector, uint8_t val)
+rtc_write_reg(u8_t reg_selector, u8_t val)
{
io_outb(RTC_INDEX_PORT, reg_selector);
io_outb(RTC_TARGET_PORT, val);
}
-uint8_t
-bcd2dec(uint8_t bcd)
+u8_t
+bcd2dec(u8_t bcd)
{
return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
}
void
rtc_enable_timer() {
- uint8_t regB = rtc_read_reg(RTC_REG_B | WITH_NMI_DISABLED);
+ u8_t regB = rtc_read_reg(RTC_REG_B | WITH_NMI_DISABLED);
rtc_write_reg(RTC_REG_B | WITH_NMI_DISABLED, regB | RTC_TIMER_ON);
}
void
rtc_disable_timer() {
- uint8_t regB = rtc_read_reg(RTC_REG_B | WITH_NMI_DISABLED);
+ u8_t regB = rtc_read_reg(RTC_REG_B | WITH_NMI_DISABLED);
rtc_write_reg(RTC_REG_B | WITH_NMI_DISABLED, regB & ~RTC_TIMER_ON);
}
\ No newline at end of file
struct exec_param
{
isr_param saved_prev_ctx;
- unsigned int vector;
- unsigned int err_code;
- unsigned int eip;
- unsigned int cs;
- unsigned int eflags;
- unsigned int esp;
- unsigned int ss;
+ u32_t vector;
+ u32_t err_code;
+ u32_t eip;
+ u32_t cs;
+ u32_t eflags;
+ u32_t esp;
+ u32_t ss;
} __attribute__((packed));
#define ISR_PARAM_SIZE sizeof(isr_param)
{
u32_t link;
u32_t esp0;
- uint16_t ss0;
- uint8_t __padding[94];
+ u16_t ss0;
+ u8_t __padding[94];
} __attribute__((packed));
void
{
u32_t signature_l;
u32_t signature_h;
- uint8_t chksum;
+ u8_t chksum;
char oem_id[6];
// Revision
- uint8_t rev;
+ u8_t rev;
acpi_rsdt_t* rsdt;
u32_t length;
acpi_sdthdr_t* xsdt;
- uint8_t x_chksum;
+ u8_t x_chksum;
char reserved[3]; // Reserved field
} __attribute__((packed)) acpi_rsdp_t;
acpi_context*
acpi_get_context();
-uint8_t
-acpi_gistranslate(uint8_t old_irq);
+u8_t
+acpi_gistranslate(u8_t old_irq);
#endif /* __LUNAIX_ACPI_ACPI_H */
acpi_sdthdr_t header;
u32_t firmware_controller_addr;
u32_t dsdt_addr;
- uint8_t reserved;
- uint8_t pm_profile;
- uint16_t sci_int;
+ u8_t reserved;
+ u8_t pm_profile;
+ u16_t sci_int;
u32_t smi_cmd_port_addr;
- uint8_t smi_acpi_enable;
- uint8_t smi_acpi_disable;
- uint8_t smi_s4bios_state;
- uint8_t smi_pstate;
+ u8_t smi_acpi_enable;
+ u8_t smi_acpi_disable;
+ u8_t smi_s4bios_state;
+ u8_t smi_pstate;
u32_t pm_reg_ports[6];
u32_t gpe0_port_addr;
u32_t gpe1_port_addr;
- uint8_t pm_reg_lens[4];
- uint8_t gpe0_len;
- uint8_t gpe1_len;
- uint8_t gpe1_base;
- uint8_t cst_cnt;
- uint16_t p_lvl2_lat;
- uint16_t p_lvl3_lat;
- uint16_t flush_size;
- uint16_t flush_stride;
- uint8_t duty_offset;
- uint8_t duty_width;
- uint8_t time_info[3];
- uint16_t boot_arch;
+ u8_t pm_reg_lens[4];
+ u8_t gpe0_len;
+ u8_t gpe1_len;
+ u8_t gpe1_base;
+ u8_t cst_cnt;
+ u16_t p_lvl2_lat;
+ u16_t p_lvl3_lat;
+ u16_t flush_size;
+ u16_t flush_stride;
+ u8_t duty_offset;
+ u8_t duty_width;
+ u8_t time_info[3];
+ u16_t boot_arch;
} ACPI_TABLE_PACKED acpi_fadt_t;
// TODO: FADT parser & support
*/
typedef struct
{
- uint8_t type;
- uint8_t length;
+ u8_t type;
+ u8_t length;
} ACPI_TABLE_PACKED acpi_ics_hdr_t;
/**
typedef struct
{
acpi_ics_hdr_t header;
- uint8_t processor_id;
- uint8_t apic_id;
+ u8_t processor_id;
+ u8_t apic_id;
u32_t flags;
} ACPI_TABLE_PACKED acpi_apic_t;
typedef struct
{
acpi_ics_hdr_t header;
- uint8_t ioapic_id;
- uint8_t reserved;
+ u8_t ioapic_id;
+ u8_t reserved;
u32_t ioapic_addr;
// The global system interrupt offset for this IOAPIC. (Kind of IRQ offset
// for a slave IOAPIC)
typedef struct
{
acpi_ics_hdr_t header;
- uint8_t bus;
+ u8_t bus;
// source, which is the original IRQ back in the era of IBM PC/AT, the 8259
// PIC
- uint8_t source;
+ u8_t source;
// global system interrupt. The override of source in APIC mode
u32_t gsi;
- uint16_t flags;
+ u16_t flags;
} ACPI_TABLE_PACKED acpi_intso_t;
typedef struct
{
u32_t base_addr_lo;
u32_t base_addr_hi;
- uint16_t pci_seg_num;
- uint8_t pci_bus_start;
- uint8_t pci_bus_end;
+ u16_t pci_seg_num;
+ u8_t pci_bus_start;
+ u8_t pci_bus_end;
u32_t reserve;
} ACPI_TABLE_PACKED;
struct mcfg_alloc_info
{
u32_t base_addr;
- uint16_t pci_seg_num;
- uint8_t pci_bus_start;
- uint8_t pci_bus_end;
+ u16_t pci_seg_num;
+ u8_t pci_bus_start;
+ u8_t pci_bus_end;
};
struct acpi_mcfg_toc
u32_t signature;
u32_t length;
// Revision
- uint8_t rev;
- uint8_t chksum;
+ u8_t rev;
+ u8_t chksum;
char oem_id[6];
char oem_table_id[8];
u32_t oem_rev;
ahci_init();
void
-ahci_parse_dev_info(struct hba_device* dev_info, uint16_t* data);
+ahci_parse_dev_info(struct hba_device* dev_info, u16_t* data);
void
-ahci_parsestr(char* str, uint16_t* reg_start, int size_word);
+ahci_parsestr(char* str, u16_t* reg_start, int size_word);
/**
* @brief Issue a HBA command (synchronized)
struct hba_cmdh
{
- uint16_t options;
- uint16_t prdt_len;
+ u16_t options;
+ u16_t prdt_len;
u32_t transferred_size;
u32_t cmd_table_base;
u32_t reserved[5];
struct hba_cmdt
{
- uint8_t command_fis[64];
- uint8_t atapi_cmd[16];
- uint8_t reserved[0x30];
+ u8_t command_fis[64];
+ u8_t atapi_cmd[16];
+ u8_t reserved[0x30];
struct hba_prdte entries[HBA_MAX_PRDTE];
} __HBA_PACKED__;
char serial_num[20];
char model[40];
u32_t flags;
- uint64_t max_lba;
+ lba_t max_lba;
u32_t block_size;
- uint64_t wwn;
- uint8_t cbd_size;
+ u64_t wwn;
+ u8_t cbd_size;
struct
{
- uint8_t sense_key;
- uint8_t error;
- uint8_t status;
- uint8_t reserve;
+ u8_t sense_key;
+ u8_t error;
+ u8_t status;
+ u8_t reserve;
} last_result;
u32_t alignment_offset;
u32_t block_per_sec;
struct sata_fis_head
{
- uint8_t type;
- uint8_t options;
- uint8_t status_cmd;
- uint8_t feat_err;
+ u8_t type;
+ u8_t options;
+ u8_t status_cmd;
+ u8_t feat_err;
} __HBA_PACKED__;
struct sata_reg_fis
{
struct sata_fis_head head;
- uint8_t lba0, lba8, lba16;
- uint8_t dev;
- uint8_t lba24, lba32, lba40;
- uint8_t feature;
+ u8_t lba0, lba8, lba16;
+ u8_t dev;
+ u8_t lba24, lba32, lba40;
+ u8_t feature;
- uint16_t count;
+ u16_t count;
- uint8_t reserved[6];
+ u8_t reserved[6];
} __HBA_PACKED__;
struct sata_data_fis
{
struct sata_fis_head head;
- uint8_t data[0];
+ u8_t data[0];
} __HBA_PACKED__;
void
sata_create_fis(struct sata_reg_fis* cmd_fis,
- uint8_t command,
- uint64_t lba,
- uint16_t sector_count);
+ u8_t command,
+ lba_t lba,
+ u16_t sector_count);
void
sata_submit(struct hba_device* dev, struct blkio_req* io_req);
struct scsi_cdb12
{
- uint8_t opcode;
- uint8_t misc1;
+ u8_t opcode;
+ u8_t misc1;
u32_t lba_be;
u32_t length;
- uint8_t misc2;
- uint8_t ctrl;
+ u8_t misc2;
+ u8_t ctrl;
} __attribute__((packed));
struct scsi_cdb16
{
- uint8_t opcode;
- uint8_t misc1;
+ u8_t opcode;
+ u8_t misc1;
u32_t lba_be_hi;
u32_t lba_be_lo;
u32_t length;
- uint8_t misc2;
- uint8_t ctrl;
+ u8_t misc2;
+ u8_t ctrl;
} __attribute__((packed));
void
scsi_create_packet12(struct scsi_cdb12* cdb,
- uint8_t opcode,
+ u8_t opcode,
u32_t lba,
u32_t alloc_size);
void
scsi_create_packet16(struct scsi_cdb16* cdb,
- uint8_t opcode,
- uint64_t lba,
+ u8_t opcode,
+ lba_t lba,
u32_t alloc_size);
void
static inline reg32
cpu_rcr0()
{
- uintptr_t val;
+ ptr_t val;
asm volatile("movl %%cr0,%0" : "=r"(val));
return val;
}
static inline reg32
cpu_rcr2()
{
- uintptr_t val;
+ ptr_t val;
asm volatile("movl %%cr2,%0" : "=r"(val));
return val;
}
static inline reg32
cpu_rcr3()
{
- uintptr_t val;
+ ptr_t val;
asm volatile("movl %%cr3,%0" : "=r"(val));
return val;
}
static inline reg32
cpu_reflags()
{
- uintptr_t val;
+ ptr_t val;
asm volatile("pushf\n"
"popl %0\n"
: "=r"(val)::);
}
static inline void
-cpu_invplg(void* va)
+cpu_invplg(ptr_t va)
{
- asm volatile("invlpg (%0)" ::"r"((uintptr_t)va) : "memory");
+ asm volatile("invlpg (%0)" ::"r"(va) : "memory");
}
static inline void
#include <lunaix/types.h>
-static inline uint8_t
+static inline u8_t
io_inb(int port)
{
- uint8_t data;
+ u8_t data;
asm volatile("inb %w1,%0" : "=a"(data) : "d"(port));
return data;
}
: "memory", "cc");
}
-static inline uint16_t
+static inline u16_t
io_inw(int port)
{
- uint16_t data;
+ u16_t data;
asm volatile("inw %w1,%0" : "=a"(data) : "d"(port));
return data;
}
}
static inline void
-io_outb(int port, uint8_t data)
+io_outb(int port, u8_t data)
{
asm volatile("outb %0, %w1" : : "a"(data), "d"(port));
}
}
static inline void
-io_outw(int port, uint16_t data)
+io_outw(int port, u16_t data)
{
asm volatile("outw %0,%w1" : : "a"(data), "d"(port));
}
ioapic_init();
void
-ioapic_write(uint8_t sel, u32_t val);
+ioapic_write(u8_t sel, u32_t val);
u32_t
-ioapic_read(uint8_t sel);
+ioapic_read(u8_t sel);
void
-ioapic_redirect(uint8_t irq, uint8_t vector, uint8_t dest, u32_t flags);
+ioapic_redirect(u8_t irq, u8_t vector, u8_t dest, u32_t flags);
#endif /* __LUNAIX_IOAPIC_H */
u32_t class_info;
u32_t cspace_base;
u32_t msi_loc;
- uint16_t intr_info;
+ u16_t intr_info;
struct
{
struct pci_driver* type;
* @return struct pci_device*
*/
struct pci_device*
-pci_get_device_by_id(uint16_t vendorId, uint16_t deviceId);
+pci_get_device_by_id(u16_t vendorId, u16_t deviceId);
/**
* @brief 初始化PCI设备的基地址寄存器。返回由该基地址代表的,
int
pci_bind_driver(struct pci_device* pci_dev);
+void
+pci_probe_bar_info(struct pci_device* device);
+
+void
+pci_probe_msi_info(struct pci_device* device);
+
#endif /* __LUNAIX_PCI_H */
#define RTC_REG_C 0xC
#define RTC_REG_D 0xD
-#define RTC_BIN_ENCODED(reg) (reg & 0x04)
-#define RTC_24HRS_ENCODED(reg) (reg & 0x02)
+#define RTC_BIN_ENCODED(reg) (reg & 0x04)
+#define RTC_24HRS_ENCODED(reg) (reg & 0x02)
-#define RTC_TIMER_BASE_FREQUENCY 1024
-#define RTC_TIMER_ON 0x40
+#define RTC_TIMER_BASE_FREQUENCY 1024
+#define RTC_TIMER_ON 0x40
-#define RTC_FREQUENCY_1024HZ 0b110
-#define RTC_DIVIDER_33KHZ (0b010 << 4)
+#define RTC_FREQUENCY_1024HZ 0b110
+#define RTC_DIVIDER_33KHZ (0b010 << 4)
void
rtc_init();
-uint8_t
-rtc_read_reg(uint8_t reg_selector);
+u8_t
+rtc_read_reg(u8_t reg_selector);
+
+u8_t
+bcd2dec(u8_t bcd);
void
-rtc_write_reg(uint8_t reg_selector, uint8_t val);
+rtc_write_reg(u8_t reg_selector, u8_t val);
void
rtc_enable_timer();
#include <stddef.h>
size_t
-__ksprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs);
+__ksprintf_internal(char* buffer,
+ const char* fmt,
+ size_t max_len,
+ va_list vargs);
size_t
ksprintf(char* buffer, char* fmt, ...);
typedef struct
{
u32_t year; // use int32 as we need to store the 4-digit year
- uint8_t month;
- uint8_t day;
- uint8_t weekday;
- uint8_t hour;
- uint8_t minute;
- uint8_t second;
+ u8_t month;
+ u8_t day;
+ u8_t weekday;
+ u8_t hour;
+ u8_t minute;
+ u8_t second;
} datetime_t;
void
#define UMMAP_END (USTACK_END - MEM_4MB)
#ifndef __ASM__
-#include <stddef.h>
+#include <lunaix/types.h>
// From Linux kernel v2.6.0 <kernel.h:194>
/**
* container_of - cast a member of a structure out to the containing structure
fifo_backone(struct fifo_buf* fbuf);
size_t
-fifo_putone(struct fifo_buf* fbuf, uint8_t data);
+fifo_putone(struct fifo_buf* fbuf, u8_t data);
size_t
-fifo_readone_async(struct fifo_buf* fbuf, uint8_t* data);
+fifo_readone_async(struct fifo_buf* fbuf, u8_t* data);
void
fifo_set_rdptr(struct fifo_buf* fbuf, size_t rdptr);
#define hashtable_init(table) \
{ \
- for (int i = 0; i < (sizeof(table) / sizeof(table[0])); i++) { \
+ for (u32_t i = 0; i < (sizeof(table) / sizeof(table[0])); i++) { \
table[i].head = 0; \
} \
}
struct elf32
{
- void* elf_file;
+ const void* elf_file;
struct elf32_ehdr eheader;
struct elf32_phdr* pheaders;
};
#define declare_elf32(elf, elf_vfile) \
struct elf32 elf = { .elf_file = elf_vfile, .pheaders = (void*)0 }
+int
+elf32_check_exec(const struct elf32* elf);
+
int
elf32_open(struct elf32* elf, const char* path);
int
-elf32_openat(struct elf32* elf, void* elf_vfile);
+elf32_openat(struct elf32* elf, const void* elf_vfile);
int
elf32_static_linked(const struct elf32* elf);
struct load_context exe;
// argv prependums
- char* argv_pp[2];
+ const char* argv_pp[2];
const char** argv;
const char** envp;
u8_t xattr_len;
iso_bbo32_t extent_addr;
iso_bbo32_t data_size;
- struct iso_datetime2 PACKED mktime; // Time the record is made, see 9.1.5
+ struct iso_datetime2 mktime; // Time the record is made, see 9.1.5
u8_t flags;
- u8_t fu_sz; // size of file unit (FU)
- u8_t gap_sz; // size of gap if FU is interleaved.
+ u8_t fu_sz; // size of file unit (FU)
+ u8_t gap_sz; // size of gap if FU is interleaved.
iso_bbo16_t vol_seq;
struct iso_var_mdu name;
} PACKED;
int
isrm_bindirq(int irq, isr_cb irq_handler);
-int
+void
isrm_bindiv(int iv, isr_cb handler);
isr_cb
struct llist_header full;
struct llist_header partial;
struct llist_header free;
- unsigned int offset;
- unsigned int piece_size;
- unsigned int cakes_count;
- unsigned int alloced_pieces;
- unsigned int pieces_per_cake;
- unsigned int pg_per_cake;
+ u32_t offset;
+ u32_t piece_size;
+ u32_t cakes_count;
+ u32_t alloced_pieces;
+ u32_t pieces_per_cake;
+ u32_t pg_per_cake;
char pile_name[PILE_NAME_MAXLEN];
pile_cb ctor;
typedef unsigned int piece_index_t;
-#define EO_FREE_PIECE (-1)
+#define EO_FREE_PIECE ((u32_t)-1)
struct cake_s
{
int
mem_map(void** addr_out,
struct mm_region** created,
- void* addr,
+ ptr_t addr,
struct v_file* file,
struct mmap_param* param);
int
-mem_unmap(ptr_t mnt, vm_regions_t* regions, void* addr, size_t length);
+mem_unmap(ptr_t mnt, vm_regions_t* regions, ptr_t addr, size_t length);
void
mem_unmap_region(ptr_t mnt, struct mm_region* region);
#include <lunaix/types.h>
void*
-ioremap(uintptr_t paddr, u32_t size);
+ioremap(ptr_t paddr, u32_t size);
-void*
-iounmap(uintptr_t vaddr, u32_t size);
+void
+iounmap(ptr_t vaddr, u32_t size);
#endif /* __LUNAIX_MMIO_H */
// 物理页地址(如果不存在映射,则为0)
ptr_t pa;
// 映射的flags
- uint16_t flags;
+ u16_t flags;
// PTE地址
x86_pte_t* pte;
} v_mapping;
typedef struct
{
x86_pte_t entry[PG_MAX_ENTRIES];
-} __attribute__((packed)) x86_page_table;
+} __attribute__((packed, aligned(4))) x86_page_table;
extern void __pg_mount_point;
* @param ppn page number
*/
void
-pmm_mark_page_free(uintptr_t ppn);
+pmm_mark_page_free(ptr_t ppn);
/**
* @brief 标注物理页为已占用
* @param ppn
*/
void
-pmm_mark_page_occupied(pid_t owner, uintptr_t ppn, pp_attr_t attr);
+pmm_mark_page_occupied(pid_t owner, ptr_t ppn, pp_attr_t attr);
/**
* @brief 标注多个连续的物理页为可用
* @param page_count 数量
*/
void
-pmm_mark_chunk_free(uintptr_t start_ppn, size_t page_count);
+pmm_mark_chunk_free(ptr_t start_ppn, size_t page_count);
/**
* @brief 标注多个连续的物理页为已占用
*
* @return void* 可用的页地址,否则为 NULL
*/
-void*
+ptr_t
pmm_alloc_page(pid_t owner, pp_attr_t attr);
/**
* @param owner
* @param num_pages 区域大小,单位为页
* @param attr
- * @return void*
+ * @return ptr_t
*/
-void*
+ptr_t
pmm_alloc_cpage(pid_t owner, size_t num_pages, pp_attr_t attr);
/**
* @param mem_upper_lim 最大可用内存地址
*/
void
-pmm_init(uintptr_t mem_upper_lim);
+pmm_init(ptr_t mem_upper_lim);
struct pp_struct*
-pmm_query(void* pa);
+pmm_query(ptr_t pa);
/**
* @brief 释放一个已分配的物理页,假若页地址不存在,则无操作。
* @return 是否成功
*/
int
-pmm_free_page(pid_t owner, void* page);
+pmm_free_page(pid_t owner, ptr_t page);
int
-pmm_ref_page(pid_t owner, void* page);
+pmm_ref_page(pid_t owner, ptr_t page);
#endif /* __LUNAIX_PMM_H */
* @return int
*/
int
-vmm_set_mapping(uintptr_t mnt,
- uintptr_t va,
- uintptr_t pa,
- pt_attr attr,
- int options);
+vmm_set_mapping(ptr_t mnt, ptr_t va, ptr_t pa, pt_attr attr, int options);
/**
* @brief 删除一个映射
* @param va
* @return int
*/
-uintptr_t
-vmm_del_mapping(uintptr_t mnt, uintptr_t va);
+ptr_t
+vmm_del_mapping(ptr_t mnt, ptr_t va);
/**
* @brief 在当前虚拟地址空间里查找一个映射
* @param mapping 映射相关属性
*/
int
-vmm_lookup(uintptr_t va, v_mapping* mapping);
+vmm_lookup(ptr_t va, v_mapping* mapping);
/**
* @brief 在指定的虚拟地址空间里查找一个映射
* @return int
*/
int
-vmm_lookupat(ptr_t mnt, uintptr_t va, v_mapping* mapping);
+vmm_lookupat(ptr_t mnt, ptr_t va, v_mapping* mapping);
/**
* @brief (COW) 为虚拟页创建副本。
* @return void* 包含虚拟页副本的物理页地址。
*
*/
-void*
-vmm_dup_page(pid_t pid, void* pa);
+ptr_t
+vmm_dup_page(pid_t pid, ptr_t pa);
-void*
+ptr_t
vmm_dup_vmspace(pid_t pid);
/**
* @brief 挂载另一个虚拟地址空间至当前虚拟地址空间
*
* @param pde 页目录的物理地址
- * @return void*
+ * @return ptr_t
*/
-void*
-vmm_mount_pd(uintptr_t mnt, void* pde);
+ptr_t
+vmm_mount_pd(ptr_t mnt, ptr_t pde);
/**
* @brief 卸载已挂载的虚拟地址空间
*
*/
-void*
-vmm_unmount_pd(uintptr_t mnt);
+ptr_t
+vmm_unmount_pd(ptr_t mnt);
void*
-vmm_ioremap(uintptr_t paddr, size_t size);
+vmm_ioremap(ptr_t paddr, size_t size);
void*
-vmm_next_free(uintptr_t start, int options);
+vmm_next_free(ptr_t start, int options);
/**
* @brief 将连续的物理地址空间映射到内核虚拟地址空间
* @return void*
*/
void*
-vmm_vmap(uintptr_t paddr, size_t size, pt_attr attr);
+vmm_vmap(ptr_t paddr, size_t size, pt_attr attr);
/**
* @brief 将当前地址空间的虚拟地址转译为物理地址。
* @param va 虚拟地址
* @return void*
*/
-void*
-vmm_v2p(void* va);
+ptr_t
+vmm_v2p(ptr_t va);
/**
* @brief 将指定地址空间的虚拟地址转译为物理地址
* @param va 虚拟地址
* @return void*
*/
-void*
-vmm_v2pat(ptr_t mnt, void* va);
+ptr_t
+vmm_v2pat(ptr_t mnt, ptr_t va);
#endif /* __LUNAIX_VMM_H */
* @param cmd
* @param args
*/
-static uint8_t
-ps2_issue_cmd(char cmd, uint16_t arg);
+static u8_t
+ps2_issue_cmd(char cmd, u16_t arg);
/**
* @brief 向PS/2控制器的编码器端口(0x60)发送指令并等待返回代码。
* @param cmd
* @param args
*/
-static uint8_t
-ps2_issue_dev_cmd(char cmd, uint16_t arg);
+static u8_t
+ps2_issue_dev_cmd(char cmd, u16_t arg);
/**
* @brief 向PS/2控制器发送指令,不等待返回代码。
* @return char
*/
static void
-ps2_post_cmd(uint8_t port, char cmd, uint16_t arg);
+ps2_post_cmd(u8_t port, char cmd, u16_t arg);
void
ps2_device_post_cmd(char cmd, char arg);
serial_init();
char
-serial_rx_byte(uintptr_t port);
+serial_rx_byte(ptr_t port);
void
-serial_rx_buffer(uintptr_t port, char* data, size_t len);
+serial_rx_buffer(ptr_t port, char* data, size_t len);
void
-serial_tx_byte(uintptr_t port, char data);
+serial_tx_byte(ptr_t port, char data);
void
-serial_tx_buffer(uintptr_t port, char* data, size_t len);
+serial_tx_buffer(ptr_t port, char* data, size_t len);
void
-serial_clear_fifo(uintptr_t port);
+serial_clear_fifo(ptr_t port);
void
-serial_disable_irq(uintptr_t port);
+serial_disable_irq(ptr_t port);
void
-serial_enable_irq(uintptr_t port);
+serial_enable_irq(ptr_t port);
#endif /* __LUNAIX_SERIAL_H */
pid_t pid; // offset = 0
struct proc_info* parent; // offset = 4
isr_param intr_ctx; // offset = 8
- uintptr_t ustack_top; // offset = 84 -> 56 -> 60
- void* page_table; // offset = 88 -> 60 -> 64
+ ptr_t ustack_top; // offset = 84 -> 56 -> 60
+ ptr_t page_table; // offset = 88 -> 60 -> 64
void* fxstate; // offset = 92 -> 64 -> 68
/* ---- critical section end ---- */
struct proc_mm mm;
time_t created;
- uint8_t state;
+ u8_t state;
int32_t exit_code;
int32_t k_status;
sigset_t sig_pending;
destroy_process(pid_t pid);
void
-setup_proc_mem(struct proc_info* proc, uintptr_t kstack_from);
+setup_proc_mem(struct proc_info* proc, ptr_t kstack_from);
/**
* @brief 复制当前进程(LunaixOS的类 fork (unix) 实现)
#define SCHED_TIME_SLICE 300
#define PROC_TABLE_SIZE 8192
-#define MAX_PROCESS (PROC_TABLE_SIZE / sizeof(uintptr_t))
+#define MAX_PROCESS (PROC_TABLE_SIZE / sizeof(ptr_t))
struct scheduler
{
struct proc_info** _procs;
int procs_index;
- unsigned int ptable_len;
+ int ptable_len;
};
void
#define _SIG_UNBLOCK SIG_UNBLOCK
#define _SIG_SETMASK SIG_SETMASK
+int
+signal_send(pid_t pid, int signum);
+
#endif /* __LUNAIX_SIGNAL_H */
#define __USER__ __attribute__((section(".usrtext")))
+#define DO_SPIN \
+ while (1) \
+ ;
+
inline static void
spin()
{
- while (1)
- ;
+ DO_SPIN
}
#ifndef __LUNAIXOS_NASSERT__
ticks_t counter;
void* payload;
void (*callback)(void*);
- uint8_t flags;
+ u8_t flags;
};
/**
timer_run_second(u32_t second,
void (*callback)(void*),
void* payload,
- uint8_t flags);
+ u8_t flags);
struct lx_timer*
timer_run_ms(u32_t millisecond,
void (*callback)(void*),
void* payload,
- uint8_t flags);
+ u8_t flags);
struct lx_timer*
-timer_run(ticks_t ticks, void (*callback)(void*), void* payload, uint8_t flags);
+timer_run(ticks_t ticks, void (*callback)(void*), void* payload, u8_t flags);
struct lx_timer_context*
timer_context();
#define __LUNAIX_TYPES_H
#include <stdarg.h>
-#include <stdint.h>
+#include <stddef.h>
#include <sys/types.h>
#define PACKED __attribute__((packed))
typedef unsigned long long u64_t;
typedef unsigned long ptr_t;
-typedef int64_t lba_t;
+typedef signed long ssize_t;
+typedef int pid_t;
+typedef unsigned long size_t;
+typedef unsigned long off_t;
+
+typedef u64_t lba_t;
#endif /* __LUNAIX_TYPES_H */
#define GDT_ENTRY 6
-uint64_t _gdt[GDT_ENTRY];
-uint16_t _gdt_limit = sizeof(_gdt) - 1;
+u64_t _gdt[GDT_ENTRY];
+u16_t _gdt_limit = sizeof(_gdt) - 1;
void
_set_gdt_entry(u32_t index, u32_t base, u32_t limit, u32_t flags)
_set_gdt_entry(2, 0, 0xfffff, SEG_R0_DATA);
_set_gdt_entry(3, 0, 0xfffff, SEG_R3_CODE);
_set_gdt_entry(4, 0, 0xfffff, SEG_R3_DATA);
- _set_gdt_entry(5, &_tss, sizeof(struct x86_tss) - 1, SEG_TSS);
+ _set_gdt_entry(5, (u32_t)&_tss, sizeof(struct x86_tss) - 1, SEG_TSS);
}
\ No newline at end of file
{
a = (a - IV_BASE) / 8;
b = (b - IV_BASE) / 8;
+
for (size_t i = a; i < b; i++) {
- char chunk = iv_bmp[i], j = 0;
+ u8_t chunk = iv_bmp[i], j = 0;
+
if (chunk == 0xff)
continue;
+
while ((chunk & 0x1)) {
chunk >>= 1;
j++;
}
+
iv_bmp[i] |= 1 << j;
+
int iv = IV_BASE + i * 8 + j;
handlers[iv] = handler ? handler : intr_routine_fallback;
+
return iv;
}
+
return 0;
}
isrm_ivfree(int iv)
{
assert(iv < 256);
+
if (iv >= IV_BASE) {
iv_bmp[(iv - IV_BASE) / 8] &= ~(1 << ((iv - IV_BASE) % 8));
}
+
handlers[iv] = intr_routine_fallback;
}
// PC_AT_IRQ_RTC -> RTC_TIMER_IV, fixed, edge trigged, polarity=high,
// physical, APIC ID 0
ioapic_redirect(acpi_gistranslate(irq), iv, 0, IOAPIC_DELMOD_FIXED);
+
return iv;
}
-int
+void
isrm_bindiv(int iv, isr_cb handler)
{
assert(iv < 256);
+
if (iv >= IV_BASE) {
iv_bmp[(iv - IV_BASE) / 8] |= 1 << ((iv - IV_BASE) % 8);
}
+
handlers[iv] = handler;
}
isrm_get(int iv)
{
assert(iv < 256);
+
return handlers[iv];
}
\ No newline at end of file
void
__print_panic_msg(const char* msg, const isr_param* param)
{
- struct exec_param* execp = param->execp;
+ volatile struct exec_param* execp = param->execp;
+
kprint_panic(" INT %u: (%x) [%p: %p] %s",
execp->vector,
execp->err_code,
#define IDT_ENTRY 256
-uint64_t _idt[IDT_ENTRY];
-uint16_t _idt_limit = sizeof(_idt) - 1;
+u64_t _idt[IDT_ENTRY];
+u16_t _idt_limit = sizeof(_idt) - 1;
static inline void
_set_idt_entry(u32_t vector,
- uint16_t seg_selector,
+ u16_t seg_selector,
void (*isr)(),
- uint8_t dpl,
- uint8_t type)
+ u8_t dpl,
+ u8_t type)
{
- uintptr_t offset = (uintptr_t)isr;
+ ptr_t offset = (ptr_t)isr;
_idt[vector] = (offset & 0xffff0000) | IDT_ATTR(dpl, type);
_idt[vector] <<= 32;
_idt[vector] |= (seg_selector << 16) | (offset & 0x0000ffff);
void
intr_routine_page_fault(const isr_param* param)
{
- uintptr_t ptr = cpu_rcr2();
+ ptr_t ptr = cpu_rcr2();
if (!ptr) {
goto segv_term;
}
}
if (!SEL_RPL(param->execp->cs)) {
- // 如果是内核页错误……
- if (do_kernel(&mapping)) {
- return;
- }
- // 如果不是,那么看看内核是不是需要用户页。
+ // TODO if kernel pfault
}
- struct mm_region* hit_region = region_get(&__current->mm.regions, ptr);
+ vm_regions_t* vmr = (vm_regions_t*)&__current->mm.regions;
+ struct mm_region* hit_region = region_get(vmr, ptr);
if (!hit_region) {
// 当你凝视深渊时……
if (PG_IS_PRESENT(*pte)) {
if ((hit_region->attr & COW_MASK) == COW_MASK) {
// normal page fault, do COW
- cpu_invplg(pte);
- uintptr_t pa =
- (uintptr_t)vmm_dup_page(__current->pid, PG_ENTRY_ADDR(*pte));
+ cpu_invplg((ptr_t)pte);
+
+ ptr_t pa = (ptr_t)vmm_dup_page(__current->pid, PG_ENTRY_ADDR(*pte));
+
pmm_free_page(__current->pid, *pte & ~0xFFF);
*pte = (*pte & 0xFFF & ~PG_DIRTY) | pa | PG_WRITE;
+
goto resolved;
}
// impossible cases or accessing privileged page
// -> a new page need to be alloc
if ((hit_region->attr & REGION_ANON)) {
if (!PG_IS_PRESENT(*pte)) {
- cpu_invplg(pte);
- uintptr_t pa = pmm_alloc_page(__current->pid, 0);
+ cpu_invplg((ptr_t)pte);
+
+ ptr_t pa = pmm_alloc_page(__current->pid, 0);
if (!pa) {
goto oom;
}
*pte = *pte | pa | PG_PRESENT;
- memset(PG_ALIGN(ptr), 0, PG_SIZE);
+ memset((void*)PG_ALIGN(ptr), 0, PG_SIZE);
goto resolved;
}
// permission denied on anon page (e.g., write on readonly page)
u32_t mseg_off = (ptr - hit_region->start);
u32_t mfile_off = mseg_off + hit_region->foff;
- uintptr_t pa = pmm_alloc_page(__current->pid, 0);
+ ptr_t pa = pmm_alloc_page(__current->pid, 0);
if (!pa) {
goto oom;
}
- cpu_invplg(pte);
+ cpu_invplg((ptr_t)pte);
*pte = (*pte & 0xFFF) | pa | PG_PRESENT;
- memset(ptr, 0, PG_SIZE);
+ memset((void*)ptr, 0, PG_SIZE);
int errno = 0;
if (mseg_off < hit_region->flen) {
- errno = file->ops->read_page(file->inode, ptr, PG_SIZE, mfile_off);
+ errno =
+ file->ops->read_page(file->inode, (void*)ptr, PG_SIZE, mfile_off);
}
if (errno < 0) {
resolved:
cpu_invplg(ptr);
return;
-}
-
-int
-do_kernel(v_mapping* mapping)
-{
- uintptr_t addr = mapping->va;
-
- // TODO
-
- return 0;
-done:
- return 1;
}
\ No newline at end of file
pushl 8(%ebp) /* ecx - #2 arg */
pushl 4(%ebp) /* ebx - #1 arg */
- call (%eax)
+ call *(%eax)
movl %eax, (%ebp) /* save the return value */
if (!bdev)
return ENODEV;
- int errno;
+ int errno = 0;
u32_t ent_lba = (u32_t)header->ents_lba;
struct gpt_entry* ents_parial = (struct gpt_entry*)valloc(GPT_BLKSIZE);
}
size_t
-fifo_putone(struct fifo_buf* fbuf, uint8_t data)
+fifo_putone(struct fifo_buf* fbuf, u8_t data)
{
mutex_lock(&fbuf->lock);
return 0;
}
- uint8_t* dest = fbuf->data;
+ u8_t* dest = fbuf->data;
dest[fbuf->wr_pos] = data;
fbuf->wr_pos = (fbuf->wr_pos + 1) % fbuf->size;
fbuf->free_len--;
}
size_t
-fifo_readone_async(struct fifo_buf* fbuf, uint8_t* data)
+fifo_readone_async(struct fifo_buf* fbuf, u8_t* data)
{
if (fbuf->free_len == fbuf->size) {
return 0;
}
- uint8_t* dest = fbuf->data;
+ u8_t* dest = fbuf->data;
*data = dest[fbuf->rd_pos];
fbuf->rd_pos = (fbuf->rd_pos + 1) % fbuf->size;
fbuf->free_len++;
void
hstrcpy(struct hstr* dest, struct hstr* src)
{
- strcpy(dest->value, src->value);
+ strcpy((char*)dest->value, src->value);
dest->hash = src->hash;
dest->len = src->len;
}
\ No newline at end of file
}
int
-elf32_openat(struct elf32* elf, void* elf_vfile)
+elf32_openat(struct elf32* elf, const void* elf_vfile)
{
int status = 0;
elf->pheaders = NULL;
int
elf32_check_exec(const struct elf32* elf)
{
- struct elf32_ehdr* ehdr = &elf->eheader;
+ const struct elf32_ehdr* ehdr = &elf->eheader;
return *(u32_t*)(ehdr->e_ident) == ELFMAGIC &&
ehdr->e_ident[EI_CLASS] == ELFCLASS32 &&
#include <lunaix/load.h>
#include <lunaix/mm/mmap.h>
#include <lunaix/mm/page.h>
+#include <lunaix/mm/valloc.h>
#include <lunaix/spike.h>
int
size_t sz_argv = exec_str_size(argv, &argv_len);
size_t sz_envp = exec_str_size(envp, &envp_len);
size_t var_sz = ROUNDUP(sz_envp, PG_SIZE);
- char** argv_extra = container->argv_pp;
+ const char** argv_extra = container->argv_pp;
argv_extra[0] = executable->dnode->name.value;
}
for (size_t i = 0; i < 2 && argv_extra[i]; i++, argv_len++) {
- char* extra_arg = argv_extra[i];
+ const char* extra_arg = argv_extra[i];
size_t str_len = strlen(extra_arg);
ustack = (void*)((ptr_t)ustack - str_len);
- memcpy(ustack, (void*)extra_arg, str_len);
+ memcpy(ustack, (const void*)extra_arg, str_len);
}
// four args (arg{c|v}, env{c|p}) for main
{
int errno = 0;
struct exec_container container;
- exec_container(&container, __current, VMS_SELF, argv, envp);
+
+ exec_container(
+ &container, (struct proc_info*)__current, VMS_SELF, argv, envp);
errno = exec_load_byname(&container, filename);
{
int errno = 0;
struct exec_container container;
- exec_container(&container, __current, VMS_SELF, argv, envp);
+
+ exec_container(
+ &container, (struct proc_info*)__current, VMS_SELF, argv, envp);
if ((errno = exec_load_byname(&container, filename))) {
goto done;
dctx->read_complete_callback(dctx, pos->name.value, pos->name.len, 0);
break;
}
+
+ return i;
}
int
u32_t l = drec->name.len;
while (l < (u32_t)-1 && drec->name.content[l--] != ';')
;
+
l = (l + 1) ? l : drec->name.len;
l = MIN(l, ISO9660_IDLEN - 1);
- strncpy(cache->name_val, drec->name.content, l);
+ strncpy(cache->name_val, (const char*)drec->name.content, l);
+
cache->name = HSTR(cache->name_val, l);
hstr_rehash(&cache->name, HSTR_FULL_HASH);
}
iso9660_unmount(struct v_superblock* vsb)
{
vfree(vsb->data);
+
+ return 0;
}
void
+#include <klibc/string.h>
#include <lunaix/fs/iso9660.h>
int
fpos += rd_bytes;
}
- return errno < 0 ? errno : buf_off;
+ return errno < 0 ? errno : (int)buf_off;
}
void
pcache_commit(struct v_inode* inode, struct pcache_pg* page)
{
if (!(page->flags & PCACHE_DIRTY)) {
- return;
+ return 0;
}
int errno =
'mountibility' for other fs.
*/
-volatile static inode_t ino = 0;
+static volatile inode_t ino = 0;
extern const struct v_inode_ops ramfs_inode_ops;
extern const struct v_file_ops ramfs_file_ops;
ramfs_inode_init(struct v_superblock* vsb, struct v_inode* inode)
{
inode->id = ino++;
- inode->ops = &ramfs_inode_ops;
- inode->default_fops = &ramfs_file_ops;
+ inode->ops = (struct v_inode_ops*)&ramfs_inode_ops;
+ inode->default_fops = (struct v_file_ops*)&ramfs_file_ops;
}
int
if ((rinode->flags & RAMF_SYMLINK)) {
rinode->flags &= ~RAMF_SYMLINK;
this->itype &= ~VFS_IFSYMLINK;
+
vfree(rinode->symlink);
- return;
+
+ return 0;
}
// TODO
void
__twifs_init_inode(struct v_superblock* vsb, struct v_inode* inode)
{
- inode->ops = &twifs_inode_ops;
- inode->default_fops = &twifs_file_ops;
+ inode->ops = (struct v_inode_ops*)&twifs_inode_ops;
+ inode->default_fops = (struct v_file_ops*)&twifs_file_ops;
}
int
atomic_fetch_add(&vfs_sysroot->ref_count, 1);
}
-inline struct hbucket*
+static inline struct hbucket*
__dcache_hash(struct v_dnode* parent, u32_t* hash)
{
u32_t _hash = *hash;
vfs_dcache_remove(pos);
}
- vfree(dnode->name.value);
+ vfree((void*)dnode->name.value);
cake_release(dnode_pile, dnode);
}
inode->atime = inode->ctime;
inode->mtime = inode->ctime;
-done:
lru_use_one(inode_lru, &inode->lru);
return inode;
}
goto done;
}
- if (errno = vfs_check_writable(file)) {
+ if ((errno = vfs_check_writable(file))) {
goto done;
}
goto done;
}
- errno = vfs_do_chdir(__current, dnode);
+ errno = vfs_do_chdir((struct proc_info*)__current, dnode);
done:
return DO_STATUS(errno);
goto done;
}
- errno = vfs_do_chdir(__current, fd_s->file->dnode);
+ errno = vfs_do_chdir((struct proc_info*)__current, fd_s->file->dnode);
done:
return DO_STATUS(errno);
return 0;
}
- if (errno = vfs_check_writable(current)) {
+ if ((errno = vfs_check_writable(current))) {
return errno;
}
errno = vfs_do_rename(cur, target);
done:
- vfree(name.value);
+ vfree((void*)name.value);
return DO_STATUS(errno);
}
\ No newline at end of file
return NULL;
}
*entry =
- (struct v_xattr_entry){ .name = HHSTR(valloc(VFS_NAME_MAXLEN), 0, 0) };
+ (struct v_xattr_entry){ .name = HHSTR(valloc(VFS_NAME_MAXLEN), 0, 0),
+ .value = NULL };
hstrcpy(&entry->name, name);
return entry;
void
xattr_free(struct v_xattr_entry* entry)
{
- vfree(entry->name.value);
+ vfree((void*)entry->name.value);
vfree(entry);
}
#include <lunaix/common.h>
-#include <lunaix/tty/tty.h>
-
#include <lunaix/device.h>
#include <lunaix/foptions.h>
#include <lunaix/input.h>
#include <lunaix/isrm.h>
#include <lunaix/lxconsole.h>
+#include <lunaix/mm/cake.h>
#include <lunaix/mm/mmio.h>
#include <lunaix/mm/page.h>
#include <lunaix/mm/pmm.h>
+#include <lunaix/mm/valloc.h>
#include <lunaix/mm/vmm.h>
#include <lunaix/process.h>
#include <lunaix/sched.h>
#include <lunaix/spike.h>
+#include <lunaix/syscall.h>
+#include <lunaix/tty/tty.h>
#include <lunaix/types.h>
#include <arch/x86/boot/multiboot.h>
#include <klibc/stdio.h>
#include <klibc/string.h>
-extern uint8_t __kernel_start;
-extern uint8_t __kernel_end;
-extern uint8_t __init_hhk_end;
+extern u8_t __kernel_start;
+extern u8_t __kernel_end;
+extern u8_t __init_hhk_end;
#define PP_KERN_SHARED (PP_FGSHARED | PP_TKERN)
// 为内核创建一个专属栈空间。
for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) {
- uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
+ ptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
vmm_set_mapping(VMS_SELF,
KSTACK_START + (i << PG_SIZE_BITS),
pa,
(struct exec_param*)(KSTACK_TOP - sizeof(struct exec_param));
*execp = (struct exec_param){ .cs = KCODE_SEG,
- .eip = (void*)__proc0,
+ .eip = (ptr_t)__proc0,
.ss = KDATA_SEG,
.eflags = cpu_reflags() };
proc0->intr_ctx.execp = execp;
assert_msg(0, "Unexpected Return");
}
-extern void __usrtext_start;
-extern void __usrtext_end;
+extern u8_t __usrtext_start;
+extern u8_t __usrtext_end;
// 按照 Memory map 标识可用的物理页
void
multiboot_memory_map_t mmap = map[i];
if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
// 整数向上取整除法
- uintptr_t pg = map[i].addr_low + 0x0fffU;
+ ptr_t pg = map[i].addr_low + 0x0fffU;
pmm_mark_chunk_free(pg >> PG_SIZE_BITS,
map[i].len_low >> PG_SIZE_BITS);
}
size_t pg_count = V2P(&__kernel_end) >> PG_SIZE_BITS;
pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED);
- for (uintptr_t i = &__usrtext_start; i < &__usrtext_end; i += PG_SIZE) {
+ for (ptr_t i = (ptr_t)&__usrtext_start; i < (ptr_t)&__usrtext_end;
+ i += PG_SIZE) {
vmm_set_mapping(VMS_SELF, i, V2P(i), PG_PREM_UR, VMAP_NULL);
}
void*
__alloc_cake(unsigned int cake_pg)
{
- uintptr_t pa = pmm_alloc_cpage(KERNEL_PID, cake_pg, 0);
+ ptr_t pa = (ptr_t)pmm_alloc_cpage(KERNEL_PID, cake_pg, 0);
if (!pa) {
return NULL;
}
return NULL;
}
- int max_piece = pile->pieces_per_cake;
+ u32_t max_piece = pile->pieces_per_cake;
- cake->first_piece = (void*)((uintptr_t)cake + pile->offset);
+ assert(max_piece);
+
+ cake->first_piece = (void*)((ptr_t)cake + pile->offset);
cake->next_free = 0;
pile->cakes_count++;
}
void* ptr =
- (void*)((uintptr_t)pos->first_piece + found_index * pile->piece_size);
+ (void*)((ptr_t)pos->first_piece + found_index * pile->piece_size);
if (pile->ctor) {
pile->ctor(pile, ptr);
if (pos->first_piece > area) {
continue;
}
- piece_index =
- (uintptr_t)(area - pos->first_piece) / pile->piece_size;
+ piece_index = (ptr_t)(area - pos->first_piece) / pile->piece_size;
if (piece_index < pile->pieces_per_cake) {
goto found;
}
+#include <lunaix/mm/pmm.h>
#include <lunaix/mm/vmm.h>
-void*
-vmm_dup_page(pid_t pid, void* pa)
+ptr_t
+vmm_dup_page(pid_t pid, ptr_t pa)
{
- void* new_ppg = pmm_alloc_page(pid, 0);
+ ptr_t new_ppg = pmm_alloc_page(pid, 0);
vmm_set_mapping(VMS_SELF, PG_MOUNT_3, new_ppg, PG_PREM_RW, VMAP_NULL);
vmm_set_mapping(VMS_SELF, PG_MOUNT_4, pa, PG_PREM_RW, VMAP_NULL);
heap->region_copied = __heap_copied;
mm_index((void**)&pvms->heap, heap);
+
+ return status;
}
__DEFINE_LXSYSCALL1(void*, sbrk, ssize_t, incr)
{
- struct proc_mm* pvms = &__current->mm;
+ struct proc_mm* pvms = (struct proc_mm*)&__current->mm;
struct mm_region* heap = pvms->heap;
assert(heap);
__DEFINE_LXSYSCALL1(int, brk, void*, addr)
{
- struct proc_mm* pvms = &__current->mm;
+ struct proc_mm* pvms = (struct proc_mm*)&__current->mm;
struct mm_region* heap = pvms->heap;
if (!heap) {
- return DO_STATUS(create_heap(pvms, addr));
+ return DO_STATUS(create_heap(pvms, (ptr_t)addr));
}
assert(heap);
int
mem_map(void** addr_out,
struct mm_region** created,
- void* addr,
+ ptr_t addr,
struct v_file* file,
struct mmap_param* param)
{
assert_msg(addr, "addr can not be NULL");
- ptr_t last_end = USER_START, found_loc = (ptr_t)addr;
+ ptr_t last_end = USER_START, found_loc = addr;
struct mm_region *pos, *n;
vm_regions_t* vm_regions = ¶m->pvms->regions;
}
if (addr_out) {
- *addr_out = found_loc;
+ *addr_out = (void*)found_loc;
}
if (created) {
*created = region;
struct v_file* file,
struct mmap_param* param)
{
+ // TODO
+
+ return EINVAL;
}
void
if (PG_IS_DIRTY(*mapping.pte)) {
size_t offset = mapping.va - region->start + region->foff;
struct v_inode* inode = region->mfile->inode;
- region->mfile->ops->write_page(inode, mapping.va, PG_SIZE, offset);
+
+ region->mfile->ops->write_page(
+ inode, (void*)mapping.va, PG_SIZE, offset);
+
*mapping.pte &= ~PG_DIRTY;
- cpu_invplg(mapping.pte);
+
+ cpu_invplg((ptr_t)mapping.pte);
} else if ((options & MS_INVALIDATE)) {
goto invalidate;
}
invalidate:
*mapping.pte &= ~PG_PRESENT;
pmm_free_page(KERNEL_PID, mapping.pa);
- cpu_invplg(mapping.pte);
+ cpu_invplg((ptr_t)mapping.pte);
}
}
}
int
-mem_unmap(ptr_t mnt, vm_regions_t* regions, void* addr, size_t length)
+mem_unmap(ptr_t mnt, vm_regions_t* regions, ptr_t addr, size_t length)
{
length = ROUNDUP(length, PG_SIZE);
ptr_t cur_addr = PG_ALIGN(addr);
int errno = 0;
void* result = (void*)-1;
- if (!length || length > BS_SIZE || !PG_ALIGNED(addr)) {
+ ptr_t addr_ptr = (ptr_t)addr;
+
+ if (!length || length > BS_SIZE || !PG_ALIGNED(addr_ptr)) {
errno = EINVAL;
goto done;
}
- if (!addr) {
- addr = UMMAP_START;
- } else if (addr < UMMAP_START || addr + length >= UMMAP_END) {
+ if (!addr_ptr) {
+ addr_ptr = UMMAP_START;
+ } else if (addr_ptr < UMMAP_START || addr_ptr + length >= UMMAP_END) {
if (!(options & (MAP_FIXED | MAP_FIXED_NOREPLACE))) {
errno = ENOMEM;
goto done;
.offset = offset,
.type = REGION_TYPE_GENERAL,
.proct = proct,
- .pvms = &__current->mm,
+ .pvms = (struct proc_mm*)&__current->mm,
.vms_mnt = VMS_SELF };
- errno = mem_map(&result, NULL, addr, file, ¶m);
+ errno = mem_map(&result, NULL, addr_ptr, file, ¶m);
done:
__current->k_status = errno;
return result;
}
-__DEFINE_LXSYSCALL2(void, munmap, void*, addr, size_t, length)
+__DEFINE_LXSYSCALL2(int, munmap, void*, addr, size_t, length)
{
- return mem_unmap(VMS_SELF, &__current->mm.regions, addr, length);
+ return mem_unmap(
+ VMS_SELF, (vm_regions_t*)&__current->mm.regions, (ptr_t)addr, length);
}
__DEFINE_LXSYSCALL3(int, msync, void*, addr, size_t, length, int, flags)
return DO_STATUS(EINVAL);
}
- int status =
- mem_msync(VMS_SELF, &__current->mm.regions, addr, length, flags);
+ int status = mem_msync(VMS_SELF,
+ (vm_regions_t*)&__current->mm.regions,
+ (ptr_t)addr,
+ length,
+ flags);
return DO_STATUS(status);
}
\ No newline at end of file
#include <lunaix/spike.h>
void*
-ioremap(uintptr_t paddr, u32_t size)
+ioremap(ptr_t paddr, u32_t size)
{
void* ptr = vmm_vmap(paddr, size, PG_PREM_RW | PG_DISABLE_CACHE);
+
if (ptr) {
pmm_mark_chunk_occupied(KERNEL_PID,
paddr >> PG_SIZE_BITS,
CEIL(size, PG_SIZE_BITS),
PP_FGLOCKED);
}
+
return ptr;
}
-void*
-iounmap(uintptr_t vaddr, u32_t size)
+void
+iounmap(ptr_t vaddr, u32_t size)
{
for (size_t i = 0; i < size; i += PG_SIZE) {
- uintptr_t paddr = vmm_del_mapping(VMS_SELF, vaddr + i);
+ ptr_t paddr = vmm_del_mapping(VMS_SELF, vaddr + i);
pmm_free_page(KERNEL_PID, paddr);
}
}
\ No newline at end of file
// This is a very large array...
static struct pp_struct pm_table[PM_BMP_MAX_SIZE];
-static uintptr_t max_pg;
+static ptr_t max_pg;
void
-pmm_mark_page_free(uintptr_t ppn)
+pmm_mark_page_free(ptr_t ppn)
{
pm_table[ppn].ref_counts = 0;
}
void
-pmm_mark_page_occupied(pid_t owner, uintptr_t ppn, pp_attr_t attr)
+pmm_mark_page_occupied(pid_t owner, ptr_t ppn, pp_attr_t attr)
{
pm_table[ppn] =
(struct pp_struct){ .owner = owner, .ref_counts = 1, .attr = attr };
}
void
-pmm_mark_chunk_free(uintptr_t start_ppn, size_t page_count)
+pmm_mark_chunk_free(ptr_t start_ppn, size_t page_count)
{
for (size_t i = start_ppn; i < start_ppn + page_count && i < max_pg; i++) {
pm_table[i].ref_counts = 0;
volatile size_t pg_lookup_ptr;
void
-pmm_init(uintptr_t mem_upper_lim)
+pmm_init(ptr_t mem_upper_lim)
{
max_pg = (PG_ALIGN(mem_upper_lim) >> 12);
}
}
-void*
+ptr_t
pmm_alloc_cpage(pid_t owner, size_t num_pages, pp_attr_t attr)
{
size_t p1 = 0;
}
if (p2 == max_pg && p2 - p1 < num_pages) {
- return NULL;
+ return NULLPTR;
}
pmm_mark_chunk_occupied(owner, p1, num_pages, attr);
return p1 << 12;
}
-void*
+ptr_t
pmm_alloc_page(pid_t owner, pp_attr_t attr)
{
// Next fit approach. Maximize the throughput!
- uintptr_t good_page_found = (uintptr_t)NULL;
+ ptr_t good_page_found = (ptr_t)NULL;
size_t old_pg_ptr = pg_lookup_ptr;
size_t upper_lim = max_pg;
struct pp_struct* pm;
if (!good_page_found) {
__current->k_status = LXOUTOFMEM;
}
- return (void*)good_page_found;
+ return good_page_found;
}
int
-pmm_free_page(pid_t owner, void* page)
+pmm_free_page(pid_t owner, ptr_t page)
{
- struct pp_struct* pm = &pm_table[(intptr_t)page >> 12];
+ struct pp_struct* pm = &pm_table[page >> 12];
// Is this a MMIO mapping or double free?
- if (((intptr_t)page >> 12) >= max_pg || !(pm->ref_counts)) {
+ if ((page >> 12) >= max_pg || !(pm->ref_counts)) {
return 0;
}
}
int
-pmm_ref_page(pid_t owner, void* page)
+pmm_ref_page(pid_t owner, ptr_t page)
{
- (void*)owner; // TODO: do smth with owner
+ (void)owner; // TODO: do smth with owner
- u32_t ppn = (uintptr_t)page >> 12;
+ u32_t ppn = page >> 12;
if (ppn >= PM_BMP_MAX_SIZE) {
return 0;
}
struct pp_struct*
-pmm_query(void* pa)
+pmm_query(ptr_t pa)
{
- u32_t ppn = (uintptr_t)pa >> 12;
+ u32_t ppn = pa >> 12;
if (ppn >= PM_BMP_MAX_SIZE) {
return NULL;
#define VMAP_START PG_MOUNT_BASE + MEM_4MB
#define VMAP_END VMS_SELF
-static uintptr_t start = VMAP_START;
+static ptr_t start = VMAP_START;
void*
-vmm_vmap(uintptr_t paddr, size_t size, pt_attr attr)
+vmm_vmap(ptr_t paddr, size_t size, pt_attr attr)
{
// next fit
assert_msg((paddr & 0xfff) == 0, "vmap: bad alignment");
size = ROUNDUP(size, PG_SIZE);
- uintptr_t current_addr = start;
+ ptr_t current_addr = start;
size_t examed_size = 0, wrapped = 0;
x86_page_table* pd = (x86_page_table*)L1_BASE_VADDR;
return NULL;
done:
- uintptr_t alloc_begin = current_addr - examed_size;
+ ptr_t alloc_begin = current_addr - examed_size;
for (size_t i = 0; i < size; i += PG_SIZE) {
vmm_set_mapping(VMS_SELF, alloc_begin + i, paddr + i, PG_PREM_RW, 0);
pmm_ref_page(KERNEL_PID, paddr + i);
}
int
-vmm_set_mapping(uintptr_t mnt,
- uintptr_t va,
- uintptr_t pa,
- pt_attr attr,
- int options)
+vmm_set_mapping(ptr_t mnt, ptr_t va, ptr_t pa, pt_attr attr, int options)
{
- assert((uintptr_t)va % PG_SIZE == 0);
+ assert((ptr_t)va % PG_SIZE == 0);
- uintptr_t l1_inx = L1_INDEX(va);
- uintptr_t l2_inx = L2_INDEX(va);
+ ptr_t l1_inx = L1_INDEX(va);
+ ptr_t l2_inx = L2_INDEX(va);
x86_page_table* l1pt = (x86_page_table*)(mnt | (1023 << 12));
x86_page_table* l2pt = (x86_page_table*)(mnt | (l1_inx << 12));
assert(attr <= 128);
if (!l1pt->entry[l1_inx]) {
- x86_page_table* new_l1pt_pa = pmm_alloc_page(KERNEL_PID, PP_FGPERSIST);
+ x86_page_table* new_l1pt_pa =
+ (x86_page_table*)pmm_alloc_page(KERNEL_PID, PP_FGPERSIST);
// 物理内存已满!
if (!new_l1pt_pa) {
NEW_L1_ENTRY(attr | PG_WRITE | PG_PRESENT, new_l1pt_pa);
// make sure our new l2 table is visible to CPU
- cpu_invplg(l2pt);
+ cpu_invplg((ptr_t)l2pt);
memset((void*)l2pt, 0, PG_SIZE);
} else {
return 1;
}
-uintptr_t
-vmm_del_mapping(uintptr_t mnt, uintptr_t va)
+ptr_t
+vmm_del_mapping(ptr_t mnt, ptr_t va)
{
- assert(((uintptr_t)va & 0xFFFU) == 0);
+ assert(((ptr_t)va & 0xFFFU) == 0);
u32_t l1_index = L1_INDEX(va);
u32_t l2_index = L2_INDEX(va);
}
int
-vmm_lookup(uintptr_t va, v_mapping* mapping)
+vmm_lookup(ptr_t va, v_mapping* mapping)
{
return vmm_lookupat(VMS_SELF, va, mapping);
}
int
-vmm_lookupat(ptr_t mnt, uintptr_t va, v_mapping* mapping)
+vmm_lookupat(ptr_t mnt, ptr_t va, v_mapping* mapping)
{
u32_t l1_index = L1_INDEX(va);
u32_t l2_index = L2_INDEX(va);
if (l1pte) {
x86_pte_t* l2pte =
&((x86_page_table*)(mnt | (l1_index << 12)))->entry[l2_index];
+
if (l2pte) {
mapping->flags = PG_ENTRY_FLAGS(*l2pte);
mapping->pa = PG_ENTRY_ADDR(*l2pte);
return 1;
}
}
+
return 0;
}
-void*
-vmm_v2p(void* va)
+ptr_t
+vmm_v2p(ptr_t va)
{
u32_t l1_index = L1_INDEX(va);
u32_t l2_index = L2_INDEX(va);
if (l1pte) {
x86_pte_t* l2pte =
&((x86_page_table*)L2_VADDR(l1_index))->entry[l2_index];
+
if (l2pte) {
- return PG_ENTRY_ADDR(*l2pte) | ((uintptr_t)va & 0xfff);
+ return PG_ENTRY_ADDR(*l2pte) | ((ptr_t)va & 0xfff);
}
}
return 0;
}
-void*
-vmm_v2pat(ptr_t mnt, void* va)
+ptr_t
+vmm_v2pat(ptr_t mnt, ptr_t va)
{
u32_t l1_index = L1_INDEX(va);
u32_t l2_index = L2_INDEX(va);
if (l1pte) {
x86_pte_t* l2pte =
&((x86_page_table*)(mnt | (l1_index << 12)))->entry[l2_index];
+
if (l2pte) {
- return PG_ENTRY_ADDR(*l2pte) | ((uintptr_t)va & 0xfff);
+ return PG_ENTRY_ADDR(*l2pte) | ((ptr_t)va & 0xfff);
}
}
return 0;
}
-void*
-vmm_mount_pd(uintptr_t mnt, void* pde)
+ptr_t
+vmm_mount_pd(ptr_t mnt, ptr_t pde)
{
x86_page_table* l1pt = (x86_page_table*)L1_BASE_VADDR;
l1pt->entry[(mnt >> 22)] = NEW_L1_ENTRY(T_SELF_REF_PERM, pde);
return mnt;
}
-void*
-vmm_unmount_pd(uintptr_t mnt)
+ptr_t
+vmm_unmount_pd(ptr_t mnt)
{
x86_page_table* l1pt = (x86_page_table*)L1_BASE_VADDR;
l1pt->entry[(mnt >> 22)] = 0;
cpu_invplg(mnt);
+ return mnt;
}
\ No newline at end of file
void
intr_ps2_kbd_handler(const isr_param* param);
-static uint8_t
-ps2_issue_cmd_wretry(char cmd, uint16_t arg);
+static u8_t
+ps2_issue_cmd_wretry(char cmd, u16_t arg);
void
ps2_device_post_cmd(char cmd, char arg)
*/
isrm_bindirq(PC_AT_IRQ_KBD, intr_ps2_kbd_handler);
-done:
cpu_enable_interrupt();
}
// 处理队列排头的指令
struct ps2_cmd* pending_cmd = &cmd_q.cmd_queue[cmd_q.queue_ptr];
- char result;
+ u8_t result;
int attempts = 0;
// 尝试将命令发送至PS/2键盘(通过PS/2控制器)
}
void
-kbd_buffer_key_event(kbd_keycode_t key, uint8_t scancode, kbd_kstate_t state)
+kbd_buffer_key_event(kbd_keycode_t key, u8_t scancode, kbd_kstate_t state)
{
/*
forgive me on these ugly bit-level tricks,
// check. But DO NOT. This chunk is in right place and right order. Moving
// it at your own risk This is to ensure we've cleared the output buffer
// everytime, so it won't pile up across irqs.
- uint8_t scancode = io_inb(PS2_PORT_ENC_DATA);
+ u8_t scancode = io_inb(PS2_PORT_ENC_DATA);
kbd_keycode_t key;
/*
}
}
-static uint8_t
-ps2_issue_cmd(char cmd, uint16_t arg)
+static u8_t
+ps2_issue_cmd(char cmd, u16_t arg)
{
ps2_post_cmd(PS2_PORT_CTRL_CMDREG, cmd, arg);
return io_inb(PS2_PORT_ENC_CMDREG);
}
-static uint8_t
-ps2_issue_cmd_wretry(char cmd, uint16_t arg)
+static u8_t
+ps2_issue_cmd_wretry(char cmd, u16_t arg)
{
- uint8_t r, c = 0;
+ u8_t r, c = 0;
while ((r = ps2_issue_cmd(cmd, arg)) == PS2_RESULT_NAK && c < 5) {
c++;
}
}
static void
-ps2_post_cmd(uint8_t port, char cmd, uint16_t arg)
+ps2_post_cmd(u8_t port, char cmd, u16_t arg)
{
// 等待PS/2输入缓冲区清空,这样我们才可以写入命令
while (io_inb(PS2_PORT_CTRL_STATUS) & PS2_STATUS_IFULL)
// 所有参数一律通过0x60传入。
while (io_inb(PS2_PORT_CTRL_STATUS) & PS2_STATUS_IFULL)
;
- io_outb(PS2_PORT_ENC_CMDREG, (uint8_t)(arg & 0x00ff));
+ io_outb(PS2_PORT_ENC_CMDREG, (u8_t)(arg & 0x00ff));
io_delay(PS2_DELAY);
}
}
-static uint8_t
-ps2_issue_dev_cmd(char cmd, uint16_t arg)
+static u8_t
+ps2_issue_dev_cmd(char cmd, u16_t arg)
{
ps2_post_cmd(PS2_PORT_ENC_CMDREG, cmd, arg);
LOG_MODULE("UART")
void
-serial_init_port(uintptr_t port)
+serial_init_port(ptr_t port)
{
// disable interrupt, use irq instead
io_outb(COM_RIE(port), 0);
}
char
-serial_rx_byte(uintptr_t port)
+serial_rx_byte(ptr_t port)
{
while (!(io_inb(COM_RSLINE(port)) & 0x01))
;
}
void
-serial_rx_buffer(uintptr_t port, char* data, size_t len)
+serial_rx_buffer(ptr_t port, char* data, size_t len)
{
for (size_t i = 0; i < len; i++) {
data[i] = serial_rx_byte(port);
}
void
-serial_tx_byte(uintptr_t port, char data)
+serial_tx_byte(ptr_t port, char data)
{
while (!(io_inb(COM_RSLINE(port)) & 0x20))
;
}
void
-serial_tx_buffer(uintptr_t port, char* data, size_t len)
+serial_tx_buffer(ptr_t port, char* data, size_t len)
{
for (size_t i = 0; i < len; i++) {
serial_tx_byte(port, data[i]);
}
void
-serial_clear_fifo(uintptr_t port)
+serial_clear_fifo(ptr_t port)
{
io_outb(COM_RIE(port), 0x0);
io_outb(COM_RCFIFO(port), 0x00);
}
void
-serial_disable_irq(uintptr_t port)
+serial_disable_irq(ptr_t port)
{
io_outb(COM_RIE(port), 0x0);
}
void
-serial_enable_irq(uintptr_t port)
+serial_enable_irq(ptr_t port)
{
io_outb(COM_RIE(port), 0x1);
}
\ No newline at end of file
}
}
-extern uint8_t __kernel_start; /* link/linker.ld */
-extern uint8_t __kernel_end; /* link/linker.ld */
-extern uint8_t __init_hhk_end; /* link/linker.ld */
+extern u8_t __kernel_start; /* link/linker.ld */
+extern u8_t __kernel_end; /* link/linker.ld */
+extern u8_t __init_hhk_end; /* link/linker.ld */
extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
void
unlock_reserved_memory();
// clean up
- for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
- vmm_del_mapping(VMS_SELF, (void*)i);
- pmm_free_page(KERNEL_PID, (void*)i);
+ for (size_t i = 0; i < (ptr_t)(&__init_hhk_end); i += PG_SIZE) {
+ vmm_del_mapping(VMS_SELF, (ptr_t)i);
+ pmm_free_page(KERNEL_PID, (ptr_t)i);
}
}
void
__do_reserved_memory(int unlock)
{
- multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
+ multiboot_memory_map_t* mmaps =
+ (multiboot_memory_map_t*)_k_init_mb_info->mmap_addr;
+
size_t map_size =
_k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
+
// v_mapping mapping;
for (unsigned int i = 0; i < map_size; i++) {
multiboot_memory_map_t mmap = mmaps[i];
- uint8_t* pa = PG_ALIGN(mmap.addr_low);
+ ptr_t pa = PG_ALIGN(mmap.addr_low);
+
if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
// Don't fuck up our kernel code or any free area!
continue;
}
+
size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
size_t j = 0;
+
if (!unlock) {
kprintf("mem: freeze: %p..%p type=%x\n",
pa,
pa + pg_num * PG_SIZE,
mmap.type);
+
for (; j < pg_num; j++) {
- uintptr_t _pa = pa + (j << PG_SIZE_BITS);
+ ptr_t _pa = pa + (j << PG_SIZE_BITS);
if (_pa >= KERNEL_MM_BASE) {
// Don't fuck up our kernel space!
break;
pmm_mark_page_occupied(
KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
}
+
// Save the progress for later unmapping.
mmaps[i].len_low = j * PG_SIZE;
} else {
pa,
pa + pg_num * PG_SIZE,
mmap.type);
+
for (; j < pg_num; j++) {
- uintptr_t _pa = pa + (j << PG_SIZE_BITS);
+ ptr_t _pa = pa + (j << PG_SIZE_BITS);
vmm_del_mapping(VMS_SELF, _pa);
if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
pmm_mark_page_free(_pa >> PG_SIZE_BITS);
LOG_MODULE("PROC")
-void*
-__dup_pagetable(pid_t pid, uintptr_t mount_point)
+ptr_t
+__dup_pagetable(pid_t pid, ptr_t mount_point)
{
- void* ptd_pp = pmm_alloc_page(pid, PP_FGPERSIST);
+ ptr_t ptd_pp = pmm_alloc_page(pid, PP_FGPERSIST);
vmm_set_mapping(VMS_SELF, PG_MOUNT_1, ptd_pp, PG_PREM_RW, VMAP_NULL);
- x86_page_table* ptd = PG_MOUNT_1;
+ x86_page_table* ptd = (x86_page_table*)PG_MOUNT_1;
x86_page_table* pptd = (x86_page_table*)(mount_point | (0x3FF << 12));
size_t kspace_l1inx = L1_INDEX(KERNEL_MM_BASE);
}
// 复制L2页表
- void* pt_pp = pmm_alloc_page(pid, PP_FGPERSIST);
+ ptr_t pt_pp = pmm_alloc_page(pid, PP_FGPERSIST);
vmm_set_mapping(VMS_SELF, PG_MOUNT_2, pt_pp, PG_PREM_RW, VMAP_NULL);
x86_page_table* ppt = (x86_page_table*)(mount_point | (i << 12));
- x86_page_table* pt = PG_MOUNT_2;
+ x86_page_table* pt = (x86_page_table*)PG_MOUNT_2;
for (size_t j = 0; j < PG_MAX_ENTRIES; j++) {
x86_pte_t pte = ppt->entry[j];
pt->entry[j] = pte;
}
- ptd->entry[i] = (uintptr_t)pt_pp | PG_ENTRY_FLAGS(ptde);
+ ptd->entry[i] = (ptr_t)pt_pp | PG_ENTRY_FLAGS(ptde);
}
ptd->entry[PG_MAX_ENTRIES - 1] = NEW_L1_ENTRY(T_SELF_REF_PERM, ptd_pp);
}
void
-__del_pagetable(pid_t pid, uintptr_t mount_point)
+__del_pagetable(pid_t pid, ptr_t mount_point)
{
x86_page_table* pptd = (x86_page_table*)(mount_point | (0x3FF << 12));
pmm_free_page(pid, PG_ENTRY_ADDR(pptd->entry[PG_MAX_ENTRIES - 1]));
}
-void*
+ptr_t
vmm_dup_vmspace(pid_t pid)
{
return __dup_pagetable(pid, VMS_SELF);
}
void
-__mark_region(uintptr_t start_vpn, uintptr_t end_vpn, int attr)
+__mark_region(ptr_t start_vpn, ptr_t end_vpn, int attr)
{
for (size_t i = start_vpn; i <= end_vpn; i++) {
x86_pte_t* curproc = &PTE_MOUNTED(VMS_SELF, i);
x86_pte_t* newproc = &PTE_MOUNTED(VMS_MOUNT_1, i);
- cpu_invplg(newproc);
+
+ cpu_invplg((ptr_t)newproc);
if ((attr & REGION_MODE_MASK) == REGION_RSHARED) {
// 如果读共享,则将两者的都标注为只读,那么任何写入都将会应用COW策略。
- cpu_invplg(curproc);
- cpu_invplg(i << 12);
+ cpu_invplg((ptr_t)curproc);
+ cpu_invplg((ptr_t)(i << 12));
+
*curproc = *curproc & ~PG_WRITE;
*newproc = *newproc & ~PG_WRITE;
} else {
continue;
}
- uintptr_t start_vpn = pos->start >> 12;
- uintptr_t end_vpn = pos->end >> 12;
+ ptr_t start_vpn = pos->start >> 12;
+ ptr_t end_vpn = pos->end >> 12;
__mark_region(start_vpn, end_vpn, pos->attr);
}
extern void __kernel_end;
void
-setup_proc_mem(struct proc_info* proc, uintptr_t usedMnt)
+setup_proc_mem(struct proc_info* proc, ptr_t usedMnt)
{
// copy the entire kernel page table
pid_t pid = proc->pid;
- void* pt_copy = __dup_pagetable(pid, usedMnt);
+ ptr_t pt_copy = __dup_pagetable(pid, usedMnt);
vmm_mount_pd(VMS_MOUNT_1, pt_copy); // 将新进程的页表挂载到挂载点#2
In the name of Celestia our glorious goddess, I will fucking HATE
the TLB for the rest of my LIFE!
*/
- cpu_invplg(ppte);
+ cpu_invplg((ptr_t)ppte);
x86_pte_t p = *ppte;
- void* ppa = vmm_dup_page(pid, PG_ENTRY_ADDR(p));
+ ptr_t ppa = vmm_dup_page(pid, PG_ENTRY_ADDR(p));
pmm_free_page(pid, PG_ENTRY_ADDR(p));
- *ppte = (p & 0xfff) | (uintptr_t)ppa;
+ *ppte = (p & 0xfff) | ppa;
}
// 我们不需要分配内核的区域,因为所有的内核代码和数据段只能通过系统调用来访问,任何非法的访问
*execp = (struct exec_param){
.cs = KCODE_SEG,
.eflags = cpu_reflags() | 0x0200,
- .eip = (void*)my_dummy,
+ .eip = (ptr_t)my_dummy,
.ss = KDATA_SEG,
};
block_current();
schedule();
+
+ return 0;
}
__DEFINE_LXSYSCALL1(unsigned int, alarm, unsigned int, seconds)
// from <kernel/process.c>
extern void
-__del_pagetable(pid_t pid, uintptr_t mount_point);
+__del_pagetable(pid_t pid, ptr_t mount_point);
pid_t
destroy_process(pid_t pid)
int index = pid;
if (index <= 0 || index > sched_ctx.ptable_len) {
__current->k_status = EINVAL;
- return;
+ return -1;
}
+
struct proc_info* proc = sched_ctx._procs[index];
sched_ctx._procs[index] = 0;
extern struct scheduler sched_ctx; /* kernel/sched.c */
+extern void
+_exit(int status);
+
void __USER__
default_sighandler_term(int signum)
{
return 0;
}
- uintptr_t ustack = __current->ustack_top & ~0xf;
+ ptr_t ustack = __current->ustack_top & ~0xf;
if ((int)(ustack - USTACK_END) < (int)sizeof(struct proc_sig)) {
// 用户栈没有空间存放信号上下文
__current->flags &= ~PROC_FINPAUSE;
__SIGCLEAR(__current->sig_inprogress, sig_ctx->sig_num);
schedule();
+
+ // never reach!
+ return 0;
}
__DEFINE_LXSYSCALL3(int,
// kernel/asm/x86/interrupts.c)
asm("int %0" ::"i"(LUNAIX_SYS_PANIC), "D"(buffer));
- spin(); // never reach
+ DO_SPIN // never reach
}
void
panick(const char* msg)
{
asm("int %0" ::"i"(LUNAIX_SYS_PANIC), "D"(msg));
- spin();
+ DO_SPIN
}
void
va_end(args);
asm("int %0" ::"i"(LUNAIX_SYS_PANIC), "D"(buffer));
- spin();
+ DO_SPIN
}
#include <lunaix/spike.h>
#include <lunaix/timer.h>
+#include <klibc/string.h>
+
static volatile time_t sys_time;
void
datetime->second = rtc_read_reg(RTC_REG_SEC);
} while (!clock_datatime_eq(datetime, ¤t));
- uint8_t regbv = rtc_read_reg(RTC_REG_B);
+ u8_t regbv = rtc_read_reg(RTC_REG_B);
// Convert from bcd to binary when needed
if (!RTC_BIN_ENCODED(regbv)) {
// To 24 hour format
if (!RTC_24HRS_ENCODED(regbv) && (datetime->hour >> 7)) {
- datetime->hour = (12 + datetime->hour & 0x80);
+ datetime->hour = 12 + (datetime->hour & 0x80);
}
datetime->year += RTC_CURRENT_CENTRY * 100;
// Don't optimize them! Took me an half hour to figure that out...
static volatile u32_t rtc_counter = 0;
-static volatile uint8_t apic_timer_done = 0;
+static volatile u8_t apic_timer_done = 0;
static volatile u32_t sched_ticks = 0;
static volatile u32_t sched_ticks_counter = 0;
timer_run_second(u32_t second,
void (*callback)(void*),
void* payload,
- uint8_t flags)
+ u8_t flags)
{
return timer_run(
second * timer_ctx->running_frequency, callback, payload, flags);
timer_run_ms(u32_t millisecond,
void (*callback)(void*),
void* payload,
- uint8_t flags)
+ u8_t flags)
{
return timer_run(timer_ctx->running_frequency / 1000 * millisecond,
callback,
}
struct lx_timer*
-timer_run(ticks_t ticks, void (*callback)(void*), void* payload, uint8_t flags)
+timer_run(ticks_t ticks, void (*callback)(void*), void* payload, u8_t flags)
{
struct lx_timer* timer = (struct lx_timer*)cake_grab(timer_pile);
struct lx_timer_context*
timer_context()
{
- return timer_ctx;
+ return (struct lx_timer_context*)timer_ctx;
}
\ No newline at end of file
int
__tty_read(struct device* dev, void* buf, size_t offset, size_t len);
+void
+console_write(struct console* console, u8_t* data, size_t size);
+
void
console_flush();
{
struct console* console = (struct console*)dev->underlay;
console_write(console, buf, len);
+
+ return len;
}
int
break;
}
}
+
return count + fifo_read(&console->input, buf + count, len - count);
}
}
void
-console_write(struct console* console, uint8_t* data, size_t size)
+console_write(struct console* console, u8_t* data, size_t size)
{
struct fifo_buf* fbuf = &console->output;
mutex_lock(&console->output.lock);
fifo_set_rdptr(fbuf, console->wnd_start);
- uint8_t* buffer = fbuf->data;
- uintptr_t ptr = fbuf->wr_pos;
- uintptr_t rd_ptr = fbuf->rd_pos;
+ u8_t* buffer = fbuf->data;
+ ptr_t ptr = fbuf->wr_pos;
+ ptr_t rd_ptr = fbuf->rd_pos;
char c;
for (size_t i = 0; i < size; i++) {
void
console_write_str(char* str)
{
- console_write(&lx_console, str, strlen(str));
+ console_write(&lx_console, (u8_t*)str, strlen(str));
}
void
console_write_char(char str)
{
- console_write(&lx_console, &str, 1);
+ console_write(&lx_console, (u8_t*)&str, 1);
}
void
io_outb(0x3D5, (io_inb(0x3D5) & 0xE0) | 15);
}
+void
+tty_set_cursor(u8_t x, u8_t y)
+{
+ if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
+ x = y = 0;
+ }
+ u32_t pos = y * TTY_WIDTH + x;
+ io_outb(0x3D4, 14);
+ io_outb(0x3D5, pos / 256);
+ io_outb(0x3D4, 15);
+ io_outb(0x3D5, pos % 256);
+}
+
void
tty_set_theme(vga_attribute fg, vga_attribute bg)
{
int state = 0;
int g[2] = { 0, 0 };
vga_attribute current_theme = tty_theme_color;
- while (fifo_readone_async(buf, &chr)) {
+ while (fifo_readone_async(buf, (u8_t*)&chr)) {
if (state == 0 && chr == '\033') {
state = 1;
} else if (state == 1 && chr == '[') {
tty_set_cursor(x, y);
}
-void
-tty_set_cursor(uint8_t x, uint8_t y)
-{
- if (x >= TTY_WIDTH || y >= TTY_HEIGHT) {
- x = y = 0;
- }
- u32_t pos = y * TTY_WIDTH + x;
- io_outb(0x3D4, 14);
- io_outb(0x3D5, pos / 256);
- io_outb(0x3D4, 15);
- io_outb(0x3D5, pos % 256);
-}
-
void
tty_clear_line(int line_num)
{
#define FLAG_CAPS (1 << 9)
size_t
-__ksprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs)
+__ksprintf_internal(char* buffer,
+ const char* fmt,
+ size_t max_len,
+ va_list vargs)
{
// This sprintf just a random implementation I found it on Internet . lol.
// Of course, with some modifications for porting to LunaixOS :)
base = 16;
goto format_unsigned;
case 'p':
- num = (uintptr_t)va_arg(vargs, void*);
+ num = (ptr_t)va_arg(vargs, void*);
base = 16;
flags |= FLAG_ALT | FLAG_ALT2 | FLAG_NUMERIC;
break;
#include <klibc/string.h>
-#include <stdint.h>
+#include <lunaix/types.h>
void*
memcpy(void* dest, const void* src, size_t num)
void*
memmove(void* dest, const void* src, size_t num)
{
- uint8_t* dest_ptr = (uint8_t*)dest;
- const uint8_t* src_ptr = (const uint8_t*)src;
+ u8_t* dest_ptr = (u8_t*)dest;
+ const u8_t* src_ptr = (const u8_t*)src;
if (dest_ptr < src_ptr) {
for (size_t i = 0; i < num; i++) {
*(dest_ptr + i) = *(src_ptr + i);
int
memcmp(const void* ptr1, const void* ptr2, size_t num)
{
- uint8_t* p1 = (uint8_t*)ptr1;
- uint8_t* p2 = (uint8_t*)ptr2;
+ u8_t* p1 = (u8_t*)ptr1;
+ u8_t* p2 = (u8_t*)ptr2;
for (size_t i = 0; i < num; i++) {
int diff = *(p1 + i) - *(p2 + i);
if (diff != 0) {
#define IDT_ENTRY 256
-uint64_t _idt[IDT_ENTRY];
-uint16_t _idt_limit = sizeof(_idt) - 1;
+u64_t _idt[IDT_ENTRY];
+u16_t _idt_limit = sizeof(_idt) - 1;
static inline void
_set_idt_entry(u32_t vector,
- uint16_t seg_selector,
+ u16_t seg_selector,
void (*isr)(),
- uint8_t dpl,
- uint8_t type)
+ u8_t dpl,
+ u8_t type)
{{
- uintptr_t offset = (uintptr_t)isr;
+ ptr_t offset = (ptr_t)isr;
_idt[vector] = (offset & 0xffff0000) | IDT_ATTR(dpl, type);
_idt[vector] <<= 32;
_idt[vector] |= (seg_selector << 16) | (offset & 0x0000ffff);
int
main(int argc, const char* argv[])
{
- char* path = ".";
+ const char* path = ".";
if (argc > 1) {
path = argv[1];
}
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
+#include <sys/lunaix.h>
#include <unistd.h>
char pwd[512];
while (1) {
getcwd(pwd, 512);
printf("[\033[2m%s\033[39;49m]$ ", pwd);
- size_t sz = read(stdin, buf, 511);
+ int sz = read(stdin, buf, 511);
if (sz < 0) {
printf("fail to read user input (%d)\n", geterrno());
#include <signal.h>
+#include <stdio.h>
+#include <sys/lunaix.h>
+#include <unistd.h>
void
sigchild_handler(int signum)
#ifndef __LUNAIX_SYS_TYPES_H
#define __LUNAIX_SYS_TYPES_H
+#undef NULL
#define NULL (void*)0
+#define NULLPTR 0
#define PEXITTERM 0x100
#define PEXITSTOP 0x200
#include <stddef.h>
int
-__vprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs);
+__vprintf_internal(char* buffer,
+ const char* fmt,
+ size_t max_len,
+ va_list vargs);
#endif /* __LUNAIX__MYSTDIO_H */
#define FLAG_CAPS (1 << 9)
int
-__vprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs)
+__vprintf_internal(char* buffer, const char* fmt, size_t max_len, va_list vargs)
{
// This sprintf just a random implementation I found it on Internet . lol.
// Of course, with some modifications for porting to LunaixOS :)