Repos / kernel.git / commitdiff
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e82f235)
raw | patch | inline | side by side (parent: e82f235)
| author | zhaoyanbai <zhaoyanbai@126.com> | |
| Sat, 15 Aug 2026 04:20:16 +0000 (12:20 +0800) | ||
| committer | zhaoyanbai <zhaoyanbai@126.com> | |
| Sat, 15 Aug 2026 04:20:21 +0000 (12:20 +0800) |
| include/cpuid.h | patch | blob | history | |
| kernel/ring3.S | patch | blob | history | |
| kernel/syscall.c | patch | blob | history | |
| qemu.sh | patch | blob | history |
diff --git a/include/cpuid.h b/include/cpuid.h
--- a/include/cpuid.h
+++ b/include/cpuid.h
#pragma once
+#define CPUID_FEAT_ECX_RDRAND (1u << 30)
+
typedef struct cpuid_regs {
unsigned long eax, ebx, ecx, edx;
} cpuid_regs_t;
diff --git a/kernel/ring3.S b/kernel/ring3.S
--- a/kernel/ring3.S
+++ b/kernel/ring3.S
nop
nop
+ pushl $0 # 模拟一个内核不需要的参数1
+ pushl $SYSC_TEST
+ call ring3_syscall
+ addl $8, %esp
+
pushl $0 # 模拟一个内核不需要的参数1
pushl $SYSC_RAND # 系统调用号
call ring3_syscall
diff --git a/kernel/syscall.c b/kernel/syscall.c
--- a/kernel/syscall.c
+++ b/kernel/syscall.c
#include "system.h"
#include "timer.h"
#include "clock.h"
+#include "cpuid.h"
extern void syscall_entry();
extern void init_sysc_handler_table();
}
int sysc_test() {
+ // 测试修改优先级
+ int priority = current->priority;
+ priority += 1;
+ priority = TASK_PRIORITY_LEVEL_USER + (priority % 10);
+ task_reset_priority(priority);
return 0;
}
int sysc_pause() {
}
int sysc_rand() {
+ bool has_rdrand = false;
+ cpuid_regs_t cpuid_regs = cpuid(1);
+ if (cpuid_regs.ecx & CPUID_FEAT_ECX_RDRAND) {
+ has_rdrand = true;
+ }
+
uint32_t rand = jiffies;
+ static uint32_t rand_seed = 0;
+
+ if (has_rdrand) {
+ asm volatile("rdrand %0" : "=r"(rand) : : "cc");
+ } else {
+ if (0 == rand_seed) {
+ rand_seed = jiffies;
+ int cnt = (jiffies % 32);
+ for (int i = 0; i < cnt; i++) {
+ uint32_t bit = rand_seed & 1;
+ rand_seed >>= 1;
+ rand_seed |= (bit << 31);
+ }
+ rand_seed ^= ((uint32_t)rdtsc());
+ }
+
+ rand_seed = rand_seed * 1664525u + 1013904223u;
+ rand = rand_seed;
+ }
+
return (int)rand;
}