CE implement stall command, libsystem Add stall_ms ()
All checks were successful
Build ISO image / build-and-deploy (push) Successful in 40s
Build documentation / build-and-deploy (push) Successful in 30s

This commit is contained in:
2026-04-08 15:29:52 +02:00
parent 87ccdbc6cb
commit a86d258a4b
4 changed files with 54 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
c += amd64/syscall.c
c += amd64/syscall.c \
amd64/stall.c
S += amd64/_start.S
o += amd64/_start.o \
amd64/syscall.o
amd64/syscall.o \
amd64/stall.o

30
libsystem/amd64/stall.c Normal file
View File

@@ -0,0 +1,30 @@
#include <stall.h>
#include <stdint.h>
static uint64_t stall_read_tsc (void) {
uint32_t lo, hi;
__asm__ volatile ("rdtsc" : "=a"(lo), "=d"(hi));
return ((uint64_t)hi << 32) | lo;
}
static uint64_t stall_get_tsc_freq_hz (void) {
uint32_t eax, ebx, ecx, edx;
__asm__ volatile ("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(0x15));
if (eax == 0 || ebx == 0 || ecx == 0)
return 2500000000ULL;
return (uint64_t)ecx * ebx / eax;
}
void stall_ms (uint64_t ms) {
uint64_t freq_hz = stall_get_tsc_freq_hz ();
uint64_t cycles = freq_hz / 1000;
uint64_t wait_cycles = ms * cycles;
uint64_t now = stall_read_tsc ();
while ((stall_read_tsc () - now) < wait_cycles)
__asm__ volatile ("pause" ::: "memory");
}

8
libsystem/stall.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef _LIBSYSTEM_STALL_H
#define _LIBSYSTEM_STALL_H
#include <stdint.h>
void stall_ms (uint64_t ms);
#endif // _LIBSYSTEM_STALL_H