#pragma once
+#define CPUID_FEAT_ECX_RDRAND (1u << 30)
+
typedef struct cpuid_regs {
unsigned long eax, ebx, ecx, edx;
} cpuid_regs_t;
#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;
}
-boot d \
-m 128\
-smp 2 \
- -cpu qemu32,+x2apic \
+ -cpu qemu32,+x2apic,+rdrand \
-machine q35 \
-serial tcp::6666,server,nowait \
-drive file=sata.img,format=raw,if=none,id=sata-disk \