Skip to content

Commit

Permalink
fs(procfs): add /proc/cpuinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mosmeh committed Jul 5, 2024
1 parent c4ff122 commit 6a3eac8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions kernel/fs/procfs/procfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static int procfs_item_open(file_description* desc, mode_t mode) {
procfs_item_inode* node = (procfs_item_inode*)desc->inode;
int rc = node->populate(desc, vec);
if (IS_ERR(rc)) {
vec_destroy(vec);
kfree(vec);
return rc;
}
Expand Down
54 changes: 54 additions & 0 deletions kernel/fs/procfs/root.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <common/stdlib.h>
#include <kernel/api/dirent.h>
#include <kernel/api/sys/sysmacros.h>
#include <kernel/cpu.h>
#include <kernel/fs/dentry.h>
#include <kernel/interrupts.h>
#include <kernel/kmsg.h>
Expand All @@ -18,6 +19,58 @@ static int populate_cmdline(file_description* desc, struct vec* vec) {
return vec_printf(vec, "%s\n", cmdline_get_raw());
}

static int print_flag(struct vec* vec, int feature, const char* name) {
if (!name[0]) {
// Skip empty names
return 0;
}
if (!cpu_has_feature(feature))
return 0;
return vec_printf(vec, "%s ", name);
}

static int populate_cpuinfo(file_description* desc, struct vec* vec) {
(void)desc;
struct cpu* cpu = cpu_get();

int ret =
vec_printf(vec,
"vendor_id : %s\n"
"cpu family : %u\n"
"model : %u\n"
"model name : %s\n",
cpu->vendor_id, cpu->family, cpu->model, cpu->model_name);
if (IS_ERR(ret))
return ret;

if (cpu->stepping) {
ret = vec_printf(vec, "stepping : %u\n", cpu->stepping);
if (IS_ERR(ret))
return ret;
}

const char* fpu = cpu_has_feature(X86_FEATURE_FPU) ? "yes" : "no";
ret = vec_printf(vec,
"fpu : %s\n"
"fpu_exception : %s\n"
"wp : yes\n"
"flags : ",
fpu, fpu);
if (IS_ERR(ret))
return ret;

#define F(variant, name) \
ret = print_flag(vec, X86_FEATURE_##variant, #name); \
if (IS_ERR(ret)) \
return ret;
ENUMERATE_X86_FEATURES(F)
#undef F

return vec_printf(vec,
"\naddress sizes : %u bits physical, %u bits virtual\n",
cpu->phys_addr_bits, cpu->virt_addr_bits);
}

static int populate_kallsyms(file_description* desc, struct vec* vec) {
(void)desc;
const struct symbol* symbol = NULL;
Expand Down Expand Up @@ -87,6 +140,7 @@ static int populate_version(file_description* desc, struct vec* vec) {
}

static procfs_item_def root_items[] = {
{"cpuinfo", S_IFREG, populate_cpuinfo},
{"cmdline", S_IFREG, populate_cmdline},
{"kallsyms", S_IFREG, populate_kallsyms},
{"kmsg", S_IFREG, populate_kmsg},
Expand Down

0 comments on commit 6a3eac8

Please sign in to comment.