Compare commits

..

2 Commits

Author SHA1 Message Date
48231931bd Improve ipcpipe locking 2025-10-15 23:26:14 +02:00
eefff6e492 Get RTC date 2025-10-15 23:08:23 +02:00
6 changed files with 136 additions and 1 deletions

View File

@ -60,6 +60,7 @@ SRCFILES += $(call GRABSRC, \
ipc/pipe \ ipc/pipe \
dev \ dev \
randcrypto \ randcrypto \
time \
) )
CFILES := $(call GET_CFILES, $(SRCFILES)) CFILES := $(call GET_CFILES, $(SRCFILES))

View File

@ -62,7 +62,7 @@
.endm .endm
.macro _vecintr_bodygen .macro _vecintr_bodygen
cli // cli
_push_regs _push_regs
cld cld
movq %rsp, %rdi movq %rsp, %rdi

View File

@ -13,12 +13,22 @@
#include "proc/proc.h" #include "proc/proc.h"
#include "dev/dev.h" #include "dev/dev.h"
#include "randcrypto/randcrypto.h" #include "randcrypto/randcrypto.h"
#include "time/time.h"
void log_bootinfo(void) { void log_bootinfo(void) {
char buf[100]; char buf[100];
LOG("kmain", "Memory total = %s\n", human_size(BOOT_INFO.memmap_total, buf, sizeof(buf))); LOG("kmain", "Memory total = %s\n", human_size(BOOT_INFO.memmap_total, buf, sizeof(buf)));
} }
void log_time(void) {
Time time;
time_get(&time);
timeunix_t unix = time_tounix(&time);
LOG("kmain", "TIME: %02u/%02u/%02u %02u:%02u:%02u (%lu)\n", time.day, time.month, time.year,
time.hour, time.minute, time.second,
unix);
}
static volatile LIMINE_BASE_REVISION(2); static volatile LIMINE_BASE_REVISION(2);
void kmain(void) { void kmain(void) {
@ -29,6 +39,7 @@ void kmain(void) {
bootinfo_init(); bootinfo_init();
term_init(BOOT_INFO.fb->address); term_init(BOOT_INFO.fb->address);
log_bootinfo(); log_bootinfo();
log_time();
hal_init(); hal_init();
pmm_init(); pmm_init();
hal_vmm_init(); hal_vmm_init();

View File

@ -15,7 +15,9 @@ int32_t SYSCALL4(sys_ipc_piperead, pid1, pipenum1, buffer1, len1) {
int32_t ret = E_OK; int32_t ret = E_OK;
if (pid == (uint64_t)-1) { if (pid == (uint64_t)-1) {
spinlock_acquire(&PROCS.spinlock);
pid = PROCS.current->pid; pid = PROCS.current->pid;
spinlock_release(&PROCS.spinlock);
} }
spinlock_acquire(&PROCS.spinlock); spinlock_acquire(&PROCS.spinlock);
@ -59,7 +61,9 @@ int32_t SYSCALL4(sys_ipc_pipewrite, pid1, pipenum1, buffer1, len1) {
int32_t ret = E_OK; int32_t ret = E_OK;
if (pid == (uint64_t)-1) { if (pid == (uint64_t)-1) {
spinlock_acquire(&PROCS.spinlock);
pid = PROCS.current->pid; pid = PROCS.current->pid;
spinlock_release(&PROCS.spinlock);
} }
spinlock_acquire(&PROCS.spinlock); spinlock_acquire(&PROCS.spinlock);
@ -170,7 +174,9 @@ int32_t SYSCALL4(sys_ipc_pipeconnect, pid1, pipenum1, pid2, pipenum2) {
int32_t ret = E_OK; int32_t ret = E_OK;
if (pid == (uint64_t)-1) { if (pid == (uint64_t)-1) {
spinlock_acquire(&PROCS.spinlock);
pid = PROCS.current->pid; pid = PROCS.current->pid;
spinlock_release(&PROCS.spinlock);
} }
spinlock_acquire(&PROCS.spinlock); spinlock_acquire(&PROCS.spinlock);

96
kernel/time/time.c Normal file
View File

@ -0,0 +1,96 @@
#include <stdint.h>
#include <stddef.h>
#include "time.h"
#include "hal/hal.h"
#define CMOS_PORT 0x70
#define CMOS_RETURN 0x71
#define CMOS_STATA 0x0A
#define CMOS_STATB 0x0B
#define CMOS_UIP (1<<7)
#define SECS 0x00
#define MINS 0x02
#define HRS 0x04
#define DAY 0x07
#define MONTH 0x08
#define YEAR 0x09
void time_microdelay(void) {
for (volatile size_t i = 0; i < 200; i++);
}
uint32_t time_cmosread(uint32_t reg) {
io_out8(CMOS_PORT, reg);
time_microdelay();
return io_in8(CMOS_RETURN);
}
void time_fill(Time *time) {
time->second = time_cmosread(SECS);
time->minute = time_cmosread(MINS);
time->hour = time_cmosread(HRS);
time->day = time_cmosread(DAY);
time->month = time_cmosread(MONTH);
time->year = time_cmosread(YEAR);
}
void time_get(Time *time) {
Time t1, t2;
uint32_t sb, bcd;
sb = time_cmosread(CMOS_STATB);
bcd = (sb & (1<<2)) == 0;
for (;;) {
time_fill(&t1);
if (time_cmosread(CMOS_STATA) & CMOS_UIP) {
continue;
}
time_fill(&t2);
if (hal_memcmp(&t1, &t2, sizeof(t1)) == 0) {
break;
}
}
if (bcd) {
#define CONV(x) (t1.x = ((t1.x >> 4) * 10) + (t1.x & 0xF))
CONV(second);
CONV(minute);
CONV(hour);
CONV(day);
CONV(month);
CONV(year);
#undef CONV
}
*time = t1;
time->year += 2000;
}
bool time_isleap(uint32_t y) {
return ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0));
}
const uint16_t days_before_month[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
timeunix_t time_tounix(Time *time) {
uint64_t days = 0;
for (uint32_t y = 1970; y < time->year; y++) {
days += time_isleap(y) ? 366 : 365;
}
days += days_before_month[time->month - 1];
if (time->month > 2 && time_isleap(time->year)) {
days += 1;
}
days += time->day - 1;
uint64_t secs = days * 86400;
secs += time->hour * 3600;
secs += time->minute * 60;
secs += time->second;
return secs;
}

21
kernel/time/time.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef TIME_TIME_H_
#define TIME_TIME_H_
#include <stdint.h>
#include <stddef.h>
typedef struct {
uint32_t second;
uint32_t minute;
uint32_t hour;
uint32_t day;
uint32_t month;
uint32_t year;
} Time;
typedef uint64_t timeunix_t;
void time_get(Time *time);
timeunix_t time_tounix(Time *time);
#endif // TIME_TIME_H_