toaruos/kernel/vfs/procfs.c

952 lines
25 KiB
C
Raw Normal View History

2021-05-31 04:47:02 +03:00
/**
* @file kernel/vfs/procfs.c
* @brief Extensible file-based information interface.
*
* Provides /proc and its contents, which allow userspace tools
* to query kernel status through directory and text file interfaces.
*
* The way this is implemented is a bit messy... every time a read
* request happens, we generate a text blob and then try to provide
* the part the reader actually asked for. This is susceptible to
* corruption if the layout of data changed between two read calls.
* We should probably be generating data on open and disposing of
* it on call...
*
* @copyright
2018-03-16 15:56:19 +03:00
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
2021-05-31 04:47:02 +03:00
* Copyright (C) 2014-2021 K. Lange
2018-03-16 15:56:19 +03:00
*/
2021-05-31 04:47:02 +03:00
#include <stdint.h>
#include <stddef.h>
#include <kernel/string.h>
#include <kernel/printf.h>
#include <kernel/vfs.h>
2018-03-19 05:38:11 +03:00
#include <kernel/version.h>
#include <kernel/process.h>
2018-09-12 05:45:52 +03:00
#include <kernel/pci.h>
2021-05-31 04:47:02 +03:00
#include <kernel/procfs.h>
#include <kernel/hashmap.h>
#include <kernel/time.h>
#include <kernel/syscall.h>
#include <kernel/mmu.h>
#include <kernel/misc.h>
2021-07-17 14:18:02 +03:00
#include <kernel/module.h>
#include <kernel/ksym.h>
2018-03-16 15:56:19 +03:00
#define PROCFS_STANDARD_ENTRIES (sizeof(std_entries) / sizeof(struct procfs_entry))
#define PROCFS_PROCDIR_ENTRIES (sizeof(procdir_entries) / sizeof(struct procfs_entry))
2021-05-31 04:47:02 +03:00
static fs_node_t * procfs_generic_create(const char * name, read_type_t read_func) {
2018-03-16 15:56:19 +03:00
fs_node_t * fnode = malloc(sizeof(fs_node_t));
memset(fnode, 0x00, sizeof(fs_node_t));
fnode->inode = 0;
strcpy(fnode->name, name);
fnode->uid = 0;
fnode->gid = 0;
fnode->mask = 0444;
fnode->flags = FS_FILE;
fnode->read = read_func;
fnode->write = NULL;
fnode->open = NULL;
fnode->close = NULL;
fnode->readdir = NULL;
fnode->finddir = NULL;
fnode->ctime = now();
fnode->mtime = now();
fnode->atime = now();
return fnode;
}
static ssize_t proc_cmdline_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-03-16 15:56:19 +03:00
char buf[1024];
process_t * proc = process_from_pid(node->inode);
if (!proc) {
/* wat */
return 0;
}
if (!proc->cmdline) {
2021-05-31 04:47:02 +03:00
snprintf(buf, 100, "%s", proc->name);
2018-03-16 15:56:19 +03:00
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) return 0;
2018-03-16 15:56:19 +03:00
if (size > _bsize - offset) size = _bsize - offset;
memcpy(buffer, buf + offset, size);
return size;
}
buf[0] = '\0';
char * _buf = buf;
char ** args = proc->cmdline;
while (*args) {
strcpy(_buf, *args);
_buf += strlen(_buf);
if (*(args+1)) {
strcpy(_buf, "\036");
_buf += strlen(_buf);
}
args++;
}
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) return 0;
2018-03-16 15:56:19 +03:00
if (size > _bsize - offset) size = _bsize - offset;
memcpy(buffer, buf + offset, size);
return size;
}
static ssize_t proc_status_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-03-16 15:56:19 +03:00
char buf[2048];
process_t * proc = process_from_pid(node->inode);
process_t * parent = process_get_parent(proc);
if (!proc) {
/* wat */
return 0;
}
2021-05-31 04:47:02 +03:00
char state = (proc->flags & PROC_FLAG_FINISHED) ? 'Z' :
((proc->flags & PROC_FLAG_SUSPENDED) ? 'T' :
2018-10-26 14:50:11 +03:00
(process_is_ready(proc) ? 'R' : 'S'));
2018-03-16 15:56:19 +03:00
char * name = proc->name + strlen(proc->name) - 1;
while (1) {
if (*name == '/') {
name++;
break;
}
if (name == proc->name) break;
name--;
}
/* Calculate process memory usage */
2021-05-31 04:47:02 +03:00
long mem_usage = mmu_count_user(proc->thread.page_directory->directory) * 4;
long shm_usage = mmu_count_shm(proc->thread.page_directory->directory) * 4;
long mem_permille = 1000 * (mem_usage + shm_usage) / mmu_total_memory();
snprintf(buf, 2000,
"Name:\t%s\n" /* name */
"State:\t%c\n"
"Tgid:\t%d\n" /* group ? group : pid */
"Pid:\t%d\n" /* pid */
"PPid:\t%d\n" /* parent pid */
"Pgid:\t%d\n" /* progress group id (job) */
"Sid:\t%d\n" /* session id */
2018-03-16 15:56:19 +03:00
"Uid:\t%d\n"
2021-05-31 04:47:02 +03:00
"Ueip:\t%#zx\n"
"SCid:\t%zu\n"
"SC0:\t%#zx\n"
"SC1:\t%#zx\n"
"SC2:\t%#zx\n"
"SC3:\t%#zx\n"
"SC4:\t%#zx\n"
"UserStack:\t%#zx\n"
2018-03-16 15:56:19 +03:00
"Path:\t%s\n"
2021-05-31 04:47:02 +03:00
"VmSize:\t %ld kB\n"
"RssShmem:\t %ld kB\n"
"MemPermille:\t %ld\n"
"LastCore:\t %d\n"
"TotalTime:\t %ld ms\n"
"SysTime:\t %ld ms\n"
"CpuPermille:\t %d %d %d %d\n"
2018-03-16 15:56:19 +03:00
,
name,
state,
proc->group ? proc->group : proc->id,
proc->id,
parent ? parent->id : 0,
2018-10-26 14:34:15 +03:00
proc->job,
proc->session,
2018-03-16 15:56:19 +03:00
proc->user,
2021-05-31 04:47:02 +03:00
proc->syscall_registers ? arch_user_ip(proc->syscall_registers) : 0,
proc->syscall_registers ? arch_syscall_number(proc->syscall_registers) : 0,
proc->syscall_registers ? arch_syscall_arg0(proc->syscall_registers) : 0,
proc->syscall_registers ? arch_syscall_arg1(proc->syscall_registers) : 0,
proc->syscall_registers ? arch_syscall_arg2(proc->syscall_registers) : 0,
proc->syscall_registers ? arch_syscall_arg3(proc->syscall_registers) : 0,
proc->syscall_registers ? arch_syscall_arg4(proc->syscall_registers) : 0,
proc->syscall_registers ? arch_stack_pointer(proc->syscall_registers) : 0,
proc->cmdline ? proc->cmdline[0] : "(none)",
2021-05-31 04:47:02 +03:00
mem_usage, shm_usage, mem_permille,
proc->owner,
proc->time_total / arch_cpu_mhz(),
proc->time_sys / arch_cpu_mhz(),
proc->usage[0], proc->usage[1], proc->usage[2], proc->usage[3]
2018-03-16 15:56:19 +03:00
);
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) return 0;
2018-03-16 15:56:19 +03:00
if (size > _bsize - offset) size = _bsize - offset;
memcpy(buffer, buf + offset, size);
return size;
}
static struct procfs_entry procdir_entries[] = {
{1, "cmdline", proc_cmdline_func},
{2, "status", proc_status_func},
};
2021-05-31 04:47:02 +03:00
static struct dirent * readdir_procfs_procdir(fs_node_t *node, uint64_t index) {
2018-03-16 15:56:19 +03:00
if (index == 0) {
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = 0;
strcpy(out->d_name, ".");
2018-03-16 15:56:19 +03:00
return out;
}
if (index == 1) {
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = 0;
strcpy(out->d_name, "..");
2018-03-16 15:56:19 +03:00
return out;
}
index -= 2;
if (index < PROCFS_PROCDIR_ENTRIES) {
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = procdir_entries[index].id;
strcpy(out->d_name, procdir_entries[index].name);
2018-03-16 15:56:19 +03:00
return out;
}
return NULL;
}
static fs_node_t * finddir_procfs_procdir(fs_node_t * node, char * name) {
if (!name) return NULL;
for (unsigned int i = 0; i < PROCFS_PROCDIR_ENTRIES; ++i) {
if (!strcmp(name, procdir_entries[i].name)) {
fs_node_t * out = procfs_generic_create(procdir_entries[i].name, procdir_entries[i].func);
out->inode = node->inode;
return out;
}
}
return NULL;
}
static fs_node_t * procfs_procdir_create(process_t * process) {
pid_t pid = process->id;
fs_node_t * fnode = malloc(sizeof(fs_node_t));
memset(fnode, 0x00, sizeof(fs_node_t));
fnode->inode = pid;
2021-05-31 04:47:02 +03:00
snprintf(fnode->name, 100, "%d", pid);
2018-03-16 15:56:19 +03:00
fnode->uid = 0;
fnode->gid = 0;
fnode->mask = 0555;
fnode->flags = FS_DIRECTORY;
fnode->read = NULL;
fnode->write = NULL;
fnode->open = NULL;
fnode->close = NULL;
fnode->readdir = readdir_procfs_procdir;
fnode->finddir = finddir_procfs_procdir;
fnode->nlink = 1;
fnode->ctime = process->start.tv_sec;
fnode->mtime = process->start.tv_sec;
fnode->atime = process->start.tv_sec;
return fnode;
}
static ssize_t cpuinfo_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2021-05-31 04:47:02 +03:00
char buf[4096];
size_t _bsize = 0;
#ifdef __x86_64__
for (int i = 0; i < processor_count; ++i) {
_bsize += snprintf(buf + _bsize, 1000,
"Processor: %d\n"
"Manufacturer: %s\n"
"MHz: %zd\n"
"Family: %d\n"
"Model: %d\n"
"Model name: %s\n"
"LAPIC id: %d\n"
"\n",
processor_local_data[i].cpu_id,
processor_local_data[i].cpu_manufacturer,
arch_cpu_mhz(), /* TODO Should this be per-cpu? */
processor_local_data[i].cpu_family,
processor_local_data[i].cpu_model,
processor_local_data[i].cpu_model_name,
processor_local_data[i].lapic_id
);
}
2022-01-23 04:36:46 +03:00
#elif defined(__aarch64__)
2022-02-03 02:27:33 +03:00
for (int i = 0; i < processor_count; ++i) {
_bsize += snprintf(buf + _bsize, 1000,
"Processor: %d\n"
"Implementer: %#x\n"
"Variant: %#x\n"
"Architecture: %#x\n"
"PartNum: %#x\n"
"Revision: %#x\n"
"\n",
processor_local_data[i].cpu_id,
(unsigned int)(processor_local_data[i].midr >> 24) & 0xFF,
(unsigned int)(processor_local_data[i].midr >> 20) & 0xF,
(unsigned int)(processor_local_data[i].midr >> 16) & 0xF,
(unsigned int)(processor_local_data[i].midr >> 4) & 0xFFF,
(unsigned int)(processor_local_data[i].midr >> 0) & 0xF
);
}
2021-05-31 04:47:02 +03:00
#endif
2018-05-17 12:43:39 +03:00
if ((size_t)offset > _bsize) return 0;
2018-05-17 12:43:39 +03:00
if (size > _bsize - offset) size = _bsize - offset;
memcpy(buffer, buf + offset, size);
return size;
2018-03-16 15:56:19 +03:00
}
static ssize_t meminfo_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-03-16 15:56:19 +03:00
char buf[1024];
2021-05-31 04:47:02 +03:00
size_t total = mmu_total_memory();
size_t free = total - mmu_used_memory();
size_t kheap = ((uintptr_t)sbrk(0) - 0xffffff0000000000UL) / 1024;
snprintf(buf, 1000,
"MemTotal: %zu kB\n"
"MemFree: %zu kB\n"
"KHeapUse: %zu kB\n"
2018-03-16 15:56:19 +03:00
, total, free, kheap);
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) return 0;
2018-03-16 15:56:19 +03:00
if (size > _bsize - offset) size = _bsize - offset;
memcpy(buffer, buf + offset, size);
return size;
}
2021-05-31 04:47:02 +03:00
#ifdef __x86_64__
static ssize_t pat_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-08-06 11:06:49 +03:00
char buf[1024];
2021-05-31 04:47:02 +03:00
uint32_t pat_value_low, pat_value_high;
asm volatile ( "rdmsr" : "=a" (pat_value_low), "=d" (pat_value_high): "c" (0x277) );
uint64_t pat_values = ((uint64_t)pat_value_high << 32) | (pat_value_low);
2018-08-06 11:06:49 +03:00
2021-05-31 04:47:02 +03:00
const char * pat_names[] = {
2018-08-06 11:06:49 +03:00
"uncacheable (UC)",
"write combining (WC)",
"Reserved",
"Reserved",
"write through (WT)",
"write protected (WP)",
"write back (WB)",
"uncached (UC-)"
};
int pa_0 = (pat_values >> 0) & 0x7;
int pa_1 = (pat_values >> 8) & 0x7;
int pa_2 = (pat_values >> 16) & 0x7;
int pa_3 = (pat_values >> 24) & 0x7;
int pa_4 = (pat_values >> 32) & 0x7;
int pa_5 = (pat_values >> 40) & 0x7;
int pa_6 = (pat_values >> 48) & 0x7;
int pa_7 = (pat_values >> 56) & 0x7;
2021-05-31 04:47:02 +03:00
snprintf(buf, 1000,
2018-08-06 11:06:49 +03:00
"PA0: %d %s\n"
"PA1: %d %s\n"
"PA2: %d %s\n"
"PA3: %d %s\n"
"PA4: %d %s\n"
"PA5: %d %s\n"
"PA6: %d %s\n"
"PA7: %d %s\n",
pa_0, pat_names[pa_0],
pa_1, pat_names[pa_1],
pa_2, pat_names[pa_2],
pa_3, pat_names[pa_3],
pa_4, pat_names[pa_4],
pa_5, pat_names[pa_5],
pa_6, pat_names[pa_6],
pa_7, pat_names[pa_7]
);
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) return 0;
2018-08-06 11:06:49 +03:00
if (size > _bsize - offset) size = _bsize - offset;
memcpy(buffer, buf + offset, size);
return size;
}
2021-05-31 04:47:02 +03:00
#endif
2018-08-06 11:06:49 +03:00
static ssize_t uptime_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-03-16 15:56:19 +03:00
char buf[1024];
2021-05-31 04:47:02 +03:00
unsigned long timer_ticks, timer_subticks;
relative_time(0,0,&timer_ticks,&timer_subticks);
snprintf(buf, 100, "%lu.%06lu\n", timer_ticks, timer_subticks);
2018-03-16 15:56:19 +03:00
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) return 0;
2018-03-16 15:56:19 +03:00
if (size > _bsize - offset) size = _bsize - offset;
memcpy(buffer, buf + offset, size);
return size;
}
static ssize_t cmdline_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-03-16 15:56:19 +03:00
char buf[1024];
2021-05-31 04:47:02 +03:00
const char * cmdline = arch_get_cmdline();
snprintf(buf, 1000, "%s\n", cmdline ? cmdline : "");
2018-03-16 15:56:19 +03:00
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) return 0;
2018-03-16 15:56:19 +03:00
if (size > _bsize - offset) size = _bsize - offset;
memcpy(buffer, buf + offset, size);
return size;
2021-05-31 04:47:02 +03:00
return 0;
2018-03-16 15:56:19 +03:00
}
static ssize_t version_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-03-16 15:56:19 +03:00
char buf[1024];
char version_number[512];
2021-05-31 04:47:02 +03:00
snprintf(version_number, 510,
__kernel_version_format,
2018-03-16 15:56:19 +03:00
__kernel_version_major,
__kernel_version_minor,
__kernel_version_lower,
__kernel_version_suffix);
2021-05-31 04:47:02 +03:00
snprintf(buf, 1000, "%s %s %s %s %s %s\n",
2018-03-16 15:56:19 +03:00
__kernel_name,
version_number,
__kernel_version_codename,
__kernel_build_date,
__kernel_build_time,
__kernel_arch);
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) return 0;
2018-03-16 15:56:19 +03:00
if (size > _bsize - offset) size = _bsize - offset;
memcpy(buffer, buf + offset, size);
return size;
}
static ssize_t compiler_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-03-16 15:56:19 +03:00
char buf[1024];
2021-05-31 04:47:02 +03:00
snprintf(buf, 1000, "%s\n", __kernel_compiler_version);
2018-03-16 15:56:19 +03:00
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) return 0;
2018-03-16 15:56:19 +03:00
if (size > _bsize - offset) size = _bsize - offset;
2018-09-12 14:34:25 +03:00
memcpy(buffer, buf + offset, size);
2018-03-16 15:56:19 +03:00
return size;
}
extern tree_t * fs_tree; /* kernel/fs/vfs.c */
static void mount_recurse(char * buf, tree_node_t * node, size_t height) {
/* End recursion on a blank entry */
if (!node) return;
char * tmp = malloc(512);
memset(tmp, 0, 512);
char * c = tmp;
/* Indent output */
for (uint32_t i = 0; i < height; ++i) {
2021-05-31 04:47:02 +03:00
c += snprintf(c, 5, " ");
2018-03-16 15:56:19 +03:00
}
/* Get the current process */
struct vfs_entry * fnode = (struct vfs_entry *)node->value;
/* Print the process name */
if (fnode->file) {
2021-05-31 04:47:02 +03:00
c += snprintf(c, 100, "%s → %s %p (%s, %s)", fnode->name, fnode->device, (void*)fnode->file, fnode->fs_type, fnode->file->name);
2018-03-16 15:56:19 +03:00
} else {
2021-05-31 04:47:02 +03:00
c += snprintf(c, 100, "%s → (empty)", fnode->name);
2018-03-16 15:56:19 +03:00
}
/* Linefeed */
2021-05-31 04:47:02 +03:00
snprintf(buf+strlen(buf), 100, "%s\n",tmp);
2018-03-16 15:56:19 +03:00
free(tmp);
foreach(child, node->children) {
/* Recursively print the children */
mount_recurse(buf+strlen(buf),child->value, height + 1);
}
}
static ssize_t mounts_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-03-16 15:56:19 +03:00
char * buf = malloc(4096);
buf[0] = '\0';
mount_recurse(buf, fs_tree->root, 0);
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) {
free(buf);
return 0;
}
2018-03-16 15:56:19 +03:00
if (size > _bsize - offset) size = _bsize - offset;
2018-09-12 14:34:25 +03:00
memcpy(buffer, buf + offset, size);
2018-03-16 15:56:19 +03:00
free(buf);
return size;
}
static ssize_t modules_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-07-20 13:24:37 +03:00
list_t * hash_keys = hashmap_keys(modules_get_list());
if (!hash_keys || !hash_keys->length) return 0;
char * buf = malloc(hash_keys->length * 512);
2018-07-20 13:24:37 +03:00
unsigned int soffset = 0;
foreach(_key, hash_keys) {
char * key = (char *)_key->value;
2021-07-17 14:18:02 +03:00
struct LoadedModule * mod_info = hashmap_get(modules_get_list(), key);
soffset += snprintf(&buf[soffset], 100, "%#zx %zu %zu %s\n",
2021-07-17 14:18:02 +03:00
mod_info->baseAddress,
mod_info->fileSize,
mod_info->loadedSize,
2021-07-17 14:18:02 +03:00
key);
2018-07-20 13:24:37 +03:00
}
free(hash_keys);
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) {
free(buf);
return 0;
}
2018-07-20 13:24:37 +03:00
if (size > _bsize - offset) size = _bsize - offset;
2018-09-12 14:34:25 +03:00
memcpy(buffer, buf + offset, size);
2018-07-20 13:24:37 +03:00
free(buf);
return size;
}
extern hashmap_t * fs_types; /* from kernel/fs/vfs.c */
static ssize_t filesystems_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-07-20 13:24:37 +03:00
list_t * hash_keys = hashmap_keys(fs_types);
if (!hash_keys || !hash_keys->length) return 0;
char * buf = malloc(hash_keys->length * 512);
2018-07-20 13:24:37 +03:00
unsigned int soffset = 0;
foreach(_key, hash_keys) {
char * key = (char *)_key->value;
2021-05-31 04:47:02 +03:00
soffset += snprintf(&buf[soffset], 100, "%s\n", key);
2018-07-20 13:24:37 +03:00
}
free(hash_keys);
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) {
free(buf);
return 0;
}
2018-07-20 13:24:37 +03:00
if (size > _bsize - offset) size = _bsize - offset;
2018-09-12 14:34:25 +03:00
memcpy(buffer, buf + offset, size);
2018-07-20 13:24:37 +03:00
free(buf);
return size;
}
static ssize_t loader_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
char * buf = malloc(512);
2018-07-20 13:39:12 +03:00
2021-05-31 04:47:02 +03:00
snprintf(buf, 511, "%s\n", arch_get_loader());
2018-07-20 13:39:12 +03:00
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) {
free(buf);
return 0;
}
2018-07-20 13:39:12 +03:00
if (size > _bsize - offset) size = _bsize - offset;
2018-09-12 14:34:25 +03:00
memcpy(buffer, buf + offset, size);
2018-07-20 13:39:12 +03:00
free(buf);
return size;
}
2021-05-31 04:47:02 +03:00
#ifdef __x86_64__
#include <kernel/arch/x86_64/irq.h>
#include <kernel/arch/x86_64/ports.h>
static ssize_t irq_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-07-21 10:02:39 +03:00
char * buf = malloc(4096);
unsigned int soffset = 0;
for (int i = 0; i < 16; ++i) {
2021-05-31 04:47:02 +03:00
soffset += snprintf(&buf[soffset], 100, "irq %d: ", i);
2018-07-21 10:02:39 +03:00
for (int j = 0; j < 4; ++j) {
2021-05-31 04:47:02 +03:00
const char * t = get_irq_handler(i, j);
2018-07-21 10:02:39 +03:00
if (!t) break;
2021-05-31 04:47:02 +03:00
soffset += snprintf(&buf[soffset], 100, "%s%s", j ? "," : "", t);
2018-07-21 10:02:39 +03:00
}
2021-05-31 04:47:02 +03:00
soffset += snprintf(&buf[soffset], 100, "\n");
2018-07-21 10:02:39 +03:00
}
2018-12-03 06:50:33 +03:00
outportb(0x20, 0x0b);
outportb(0xa0, 0x0b);
2021-05-31 04:47:02 +03:00
soffset += snprintf(&buf[soffset], 100, "isr=0x%04x\n", (inportb(0xA0) << 8) | inportb(0x20));
2018-12-03 06:50:33 +03:00
outportb(0x20, 0x0a);
outportb(0xa0, 0x0a);
2021-05-31 04:47:02 +03:00
soffset += snprintf(&buf[soffset], 100, "irr=0x%04x\n", (inportb(0xA0) << 8) | inportb(0x20));
2018-12-03 06:50:33 +03:00
2021-05-31 04:47:02 +03:00
soffset += snprintf(&buf[soffset], 100, "imr=0x%04x\n", (inportb(0xA1) << 8) | inportb(0x21));
2018-12-03 06:50:33 +03:00
2018-07-21 10:02:39 +03:00
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) {
2018-07-21 10:02:39 +03:00
free(buf);
return 0;
}
if (size > _bsize - offset) size = _bsize - offset;
2018-09-12 14:34:25 +03:00
memcpy(buffer, buf + offset, size);
2018-07-21 10:02:39 +03:00
free(buf);
return size;
}
2022-01-23 04:36:46 +03:00
#endif
2018-07-21 10:02:39 +03:00
2018-09-12 05:45:52 +03:00
/**
* Basically the same as the kdebug `pci` command.
*/
struct _pci_buf {
size_t offset;
char *buffer;
};
static void scan_hit_list(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
struct _pci_buf * b = extra;
2021-05-31 04:47:02 +03:00
b->offset += snprintf(b->buffer + b->offset, 100, "%02x:%02x.%d (%04x, %04x:%04x)\n",
2018-09-12 05:45:52 +03:00
(int)pci_extract_bus(device),
(int)pci_extract_slot(device),
(int)pci_extract_func(device),
(int)pci_find_type(device),
vendorid,
deviceid);
2018-09-12 05:45:52 +03:00
2021-05-31 04:47:02 +03:00
b->offset += snprintf(b->buffer + b->offset, 100, " BAR0: 0x%08x", pci_read_field(device, PCI_BAR0, 4));
b->offset += snprintf(b->buffer + b->offset, 100, " BAR1: 0x%08x", pci_read_field(device, PCI_BAR1, 4));
b->offset += snprintf(b->buffer + b->offset, 100, " BAR2: 0x%08x", pci_read_field(device, PCI_BAR2, 4));
b->offset += snprintf(b->buffer + b->offset, 100, " BAR3: 0x%08x", pci_read_field(device, PCI_BAR3, 4));
b->offset += snprintf(b->buffer + b->offset, 100, " BAR4: 0x%08x", pci_read_field(device, PCI_BAR4, 4));
b->offset += snprintf(b->buffer + b->offset, 100, " BAR5: 0x%08x\n", pci_read_field(device, PCI_BAR5, 4));
b->offset += snprintf(b->buffer + b->offset, 100, " IRQ Line: %d", pci_read_field(device, 0x3C, 1));
b->offset += snprintf(b->buffer + b->offset, 100, " IRQ Pin: %d", pci_read_field(device, 0x3D, 1));
b->offset += snprintf(b->buffer + b->offset, 100, " Interrupt: %d", pci_get_interrupt(device));
b->offset += snprintf(b->buffer + b->offset, 100, " Status: 0x%04x\n", pci_read_field(device, PCI_STATUS, 2));
2018-09-12 05:45:52 +03:00
}
static void scan_count(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
size_t * count = extra;
(*count)++;
}
static ssize_t pci_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
2018-09-12 05:45:52 +03:00
size_t count = 0;
pci_scan(&scan_count, -1, &count);
struct _pci_buf b = {0,NULL};
2018-09-12 14:34:25 +03:00
b.buffer = malloc(count * 1024);
2018-09-12 05:45:52 +03:00
pci_scan(&scan_hit_list, -1, &b);
size_t _bsize = b.offset;
if ((size_t)offset > _bsize) {
2018-09-12 05:45:52 +03:00
free(b.buffer);
return 0;
}
if (size > _bsize - offset) size = _bsize - offset;
2018-09-12 14:34:25 +03:00
memcpy(buffer, b.buffer + offset, size);
2018-09-12 05:45:52 +03:00
free(b.buffer);
return size;
}
2021-09-05 05:35:38 +03:00
static ssize_t idle_func(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
char * buf = malloc(4096);
unsigned int soffset = 0;
for (int i = 0; i < processor_count; ++i) {
2021-09-05 08:16:15 +03:00
soffset += snprintf(&buf[soffset], 100, "%d: %4d %4d %4d %4d\n",
2021-09-03 10:44:49 +03:00
i,
2021-09-05 05:35:38 +03:00
processor_local_data[i].kernel_idle_task->usage[0],
processor_local_data[i].kernel_idle_task->usage[1],
processor_local_data[i].kernel_idle_task->usage[2],
processor_local_data[i].kernel_idle_task->usage[3]
2021-09-03 10:44:49 +03:00
);
}
size_t _bsize = strlen(buf);
if ((size_t)offset > _bsize) {
free(buf);
return 0;
}
if (size > _bsize - offset) size = _bsize - offset;
memcpy(buffer, buf + offset, size);
free(buf);
return size;
}
static ssize_t kallsyms_func(fs_node_t *node, off_t offset, size_t size, uint8_t * buffer) {
/* This doesn't include module symbols at the moment... */
list_t * syms = ksym_list();
/* Figure out how much space needs to go in this buffer */
size_t total_size = 0;
foreach (node, syms) {
/* 16 for the address, one space, len(sym), one linefeed = 18 + strlen() */
total_size += 18 + strlen((char*)node->value);
}
char * buf = malloc(total_size);
size_t soffset = 0;
foreach(node, syms) {
soffset += snprintf(&buf[soffset], 100, "%016zx %s\n", this_core->current_process->user == USER_ROOT_UID ? (uintptr_t)ksym_lookup(node->value) : (uintptr_t)0, (char*)node->value);
}
if ((size_t)offset >= soffset) {
size = 0;
goto free_results;
}
if (size > soffset - offset) size = soffset - offset;
memcpy(buffer, buf + offset, size);
free_results:
list_free(syms);
free(syms);
free(buf);
return size;
}
2018-03-16 15:56:19 +03:00
static struct procfs_entry std_entries[] = {
{-1, "cpuinfo", cpuinfo_func},
{-2, "meminfo", meminfo_func},
{-3, "uptime", uptime_func},
{-4, "cmdline", cmdline_func},
{-5, "version", version_func},
{-6, "compiler", compiler_func},
{-7, "mounts", mounts_func},
{-8, "modules", modules_func},
{-9, "filesystems", filesystems_func},
{-10,"loader", loader_func},
2021-09-05 05:35:38 +03:00
{-11,"idle", idle_func},
{-12,"kallsyms", kallsyms_func},
2022-01-23 04:36:46 +03:00
{-13,"pci", pci_func},
2021-05-31 04:47:02 +03:00
#ifdef __x86_64__
2022-01-23 04:36:46 +03:00
{-14,"irq", irq_func},
{-15,"pat", pat_func},
2021-05-31 04:47:02 +03:00
#endif
2018-03-16 15:56:19 +03:00
};
static list_t * extended_entries = NULL;
2021-05-31 04:47:02 +03:00
static long next_id = 0;
int procfs_install(struct procfs_entry * entry) {
if (!extended_entries) {
2021-05-31 04:47:02 +03:00
extended_entries = list_create("procfs entries",NULL);
next_id = -PROCFS_STANDARD_ENTRIES - 1;
}
entry->id = next_id--;
list_insert(extended_entries, entry);
return 0;
}
2021-05-31 04:47:02 +03:00
static struct dirent * readdir_procfs_root(fs_node_t *node, uint64_t index) {
2018-03-16 15:56:19 +03:00
if (index == 0) {
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = 0;
strcpy(out->d_name, ".");
2018-03-16 15:56:19 +03:00
return out;
}
if (index == 1) {
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = 0;
strcpy(out->d_name, "..");
2018-03-16 15:56:19 +03:00
return out;
}
if (index == 2) {
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = 0;
strcpy(out->d_name, "self");
2018-03-16 15:56:19 +03:00
return out;
}
index -= 3;
if (index < PROCFS_STANDARD_ENTRIES) {
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = std_entries[index].id;
strcpy(out->d_name, std_entries[index].name);
2018-03-16 15:56:19 +03:00
return out;
}
index -= PROCFS_STANDARD_ENTRIES;
if (extended_entries) {
if (index < extended_entries->length) {
size_t i = 0;
node_t * n = extended_entries->head;
while (i < index) {
n = n->next;
i++;
}
struct procfs_entry * e = n->value;
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = e->id;
strcpy(out->d_name, e->name);
return out;
}
index -= extended_entries->length;
}
int i = index + 1;
2018-03-16 15:56:19 +03:00
pid_t pid = 0;
foreach(lnode, process_list) {
i--;
if (i == 0) {
process_t * proc = (process_t *)lnode->value;
pid = proc->id;
break;
}
}
if (pid == 0) {
return NULL;
}
struct dirent * out = malloc(sizeof(struct dirent));
memset(out, 0x00, sizeof(struct dirent));
2021-06-05 12:01:48 +03:00
out->d_ino = pid;
snprintf(out->d_name, 100, "%d", pid);
2018-03-16 15:56:19 +03:00
return out;
}
static ssize_t readlink_self(fs_node_t * node, char * buf, size_t size) {
char tmp[30];
size_t req;
2021-05-31 04:47:02 +03:00
snprintf(tmp, 100, "/proc/%d", this_core->current_process->id);
req = strlen(tmp) + 1;
if (size < req) {
memcpy(buf, tmp, size);
buf[size-1] = '\0';
return size-1;
}
if (size > req) size = req;
memcpy(buf, tmp, size);
return size-1;
}
static fs_node_t * procfs_create_self(void) {
fs_node_t * fnode = malloc(sizeof(fs_node_t));
memset(fnode, 0x00, sizeof(fs_node_t));
fnode->inode = 0;
strcpy(fnode->name, "self");
fnode->mask = 0777;
fnode->uid = 0;
fnode->gid = 0;
fnode->flags = FS_FILE | FS_SYMLINK;
fnode->readlink = readlink_self;
fnode->length = 1;
fnode->nlink = 1;
fnode->ctime = now();
fnode->mtime = now();
fnode->atime = now();
return fnode;
}
2018-03-16 15:56:19 +03:00
static fs_node_t * finddir_procfs_root(fs_node_t * node, char * name) {
if (!name) return NULL;
if (strlen(name) < 1) return NULL;
if (name[0] >= '0' && name[0] <= '9') {
/* XXX process entries */
pid_t pid = atoi(name);
process_t * proc = process_from_pid(pid);
if (!proc) {
return NULL;
}
fs_node_t * out = procfs_procdir_create(proc);
return out;
}
if (!strcmp(name,"self")) {
return procfs_create_self();
2018-03-16 15:56:19 +03:00
}
for (unsigned int i = 0; i < PROCFS_STANDARD_ENTRIES; ++i) {
if (!strcmp(name, std_entries[i].name)) {
fs_node_t * out = procfs_generic_create(std_entries[i].name, std_entries[i].func);
return out;
}
}
if (extended_entries) {
foreach(node, extended_entries) {
struct procfs_entry * e = node->value;
if (!strcmp(name, e->name)) {
fs_node_t * out = procfs_generic_create(e->name, e->func);
return out;
}
}
}
2018-03-16 15:56:19 +03:00
return NULL;
}
static fs_node_t * procfs_create(void) {
fs_node_t * fnode = malloc(sizeof(fs_node_t));
memset(fnode, 0x00, sizeof(fs_node_t));
fnode->inode = 0;
strcpy(fnode->name, "proc");
fnode->mask = 0555;
fnode->uid = 0;
fnode->gid = 0;
fnode->flags = FS_DIRECTORY;
fnode->read = NULL;
fnode->write = NULL;
fnode->open = NULL;
fnode->close = NULL;
fnode->readdir = readdir_procfs_root;
fnode->finddir = finddir_procfs_root;
fnode->nlink = 1;
fnode->ctime = now();
fnode->mtime = now();
fnode->atime = now();
return fnode;
}
2021-05-31 04:47:02 +03:00
void procfs_initialize(void) {
2018-03-16 15:56:19 +03:00
/* TODO Move this to some sort of config */
vfs_mount("/proc", procfs_create());
2021-05-31 04:47:02 +03:00
//debug_print_vfs_tree();
2018-03-16 15:56:19 +03:00
}