Skip to content

Commit 260651c

Browse files
Fixed cat command causing an unknown exception. Added more proc functions.
1 parent 5d494fd commit 260651c

File tree

4 files changed

+84
-14
lines changed

4 files changed

+84
-14
lines changed

source/kernel/C/filesystems/layers/proc.c

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <basics.h>
1313
#include <strings.h>
1414
#include <heap.h>
15+
#include <memory.h>
1516

1617
#define PROCFS_MAX_FILES 32
1718

@@ -86,6 +87,59 @@ static procfs_entry_t proc_heap = {
8687
.priv = NULL
8788
};
8889

90+
extern struct memory_context* limine_memory_ctx;
91+
92+
static int proc_meminfo_read(vfs_file_t* file, uint8_t* buf, uint32_t size, void* priv) {
93+
(void)priv;
94+
95+
char tmp[512];
96+
int len = 0;
97+
98+
// Convert bytes to KB
99+
uint64_t kb_total = limine_memory_ctx->total / 1024;
100+
uint64_t kb_free = limine_memory_ctx->usable / 1024;
101+
uint64_t kb_reserved = limine_memory_ctx->reserved / 1024;
102+
uint64_t kb_acpi_reclaim= limine_memory_ctx->acpi_reclaimable / 1024;
103+
uint64_t kb_acpi_nvs = limine_memory_ctx->acpi_nvs / 1024;
104+
uint64_t kb_bad = limine_memory_ctx->bad / 1024;
105+
uint64_t kb_boot = limine_memory_ctx->bootloader_reclaimable / 1024;
106+
uint64_t kb_kernel = limine_memory_ctx->kernel_modules / 1024;
107+
uint64_t kb_fb = limine_memory_ctx->framebuffer / 1024;
108+
uint64_t kb_unknown = limine_memory_ctx->unknown / 1024;
109+
110+
// Build meminfo string like Linux
111+
len += snprintf(tmp + len, sizeof(tmp) - len,
112+
"MemTotal: %u kB\n"
113+
"MemFree: %u kB\n"
114+
"MemReserved: %u kB\n"
115+
"ACPI Reclaim: %u kB\n"
116+
"ACPI NVS: %u kB\n"
117+
"BadMem: %u kB\n"
118+
"Bootloader: %u kB\n"
119+
"KernelModules: %u kB\n"
120+
"Framebuffer: %u kB\n"
121+
"Unknown: %u kB\n",
122+
kb_total, kb_free, kb_reserved, kb_acpi_reclaim,
123+
kb_acpi_nvs, kb_bad, kb_boot, kb_kernel, kb_fb, kb_unknown
124+
);
125+
126+
// Handle file offset for multiple reads
127+
if (file->pos >= (uint32_t)len) return 0;
128+
129+
uint32_t rem = len - file->pos;
130+
if (rem > size) rem = size;
131+
132+
memcpy(buf, tmp + file->pos, rem);
133+
file->pos += rem;
134+
return rem;
135+
}
136+
137+
static procfs_entry_t proc_meminfo = {
138+
.name = "meminfo",
139+
.read = proc_meminfo_read,
140+
.write = NULL,
141+
.priv = NULL
142+
};
89143

90144
/* END */
91145

@@ -94,6 +148,7 @@ void procfs_init(void) {
94148
memset(proc_files, 0, sizeof(proc_files));
95149
procfs_register(&proc_stat);
96150
procfs_register(&proc_heap);
151+
procfs_register(&proc_meminfo);
97152
}
98153

99154
/* Register a virtual proc file */
@@ -115,30 +170,40 @@ static procfs_entry_t* procfs_find(const char* name) {
115170
}
116171

117172
int procfs_open(vfs_file_t* file) {
173+
if (!file || !file->rel_path)
174+
return -1;
175+
118176
procfs_entry_t* e = procfs_find(file->rel_path);
119177
if (!e)
120-
return -1;
178+
return -1; // file doesn't exist
121179

122180
file->pos = 0;
123181
return 0;
124182
}
125183

126184
int procfs_read(vfs_file_t* file, uint8_t* buf, uint32_t size) {
185+
if (!file || !file->rel_path || !buf)
186+
return -1;
187+
127188
procfs_entry_t* e = procfs_find(file->rel_path);
128-
if (!e || !e->read)
129-
return 0;
189+
if (!e) return -1; // file not found
190+
if (!e->read) return -1; // read not supported
130191

131192
return e->read(file, buf, size, e->priv);
132193
}
133194

134195
int procfs_write(vfs_file_t* file, const uint8_t* buf, uint32_t size) {
135-
procfs_entry_t* e = procfs_find(file->rel_path);
136-
if (!e || !e->write)
196+
if (!file || !file->rel_path || !buf)
137197
return -1;
138198

199+
procfs_entry_t* e = procfs_find(file->rel_path);
200+
if (!e) return -1;
201+
if (!e->write) return -1;
202+
139203
return e->write(file, buf, size, e->priv);
140204
}
141205

206+
142207
void procfs_close(vfs_file_t* file) {
143208
(void)file;
144209
}

source/kernel/C/filesystems/vfs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@ int vfs_open(const char* path, int flags, vfs_file_t* out)
246246
strncpy(out->rel_path, res.rel_path, sizeof(out->rel_path));
247247
out->mnt = res.mnt;
248248
out->flags = flags;
249-
return procfs_open(out);
249+
250+
if (procfs_open(out) != 0) // if file not found
251+
return -1;
252+
253+
return 0;
250254
}
251255

252256
if (res.mnt->type == FS_FAT16) {

source/kernel/C/kernel.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct limine_module_request module_request = {
5050

5151
struct flanterm_context *ft_ctx = null;
5252
struct limine_framebuffer *framebuffer = null;
53+
struct memory_context* limine_memory_ctx;
5354

5455
bool isBufferReady = no;
5556

@@ -159,7 +160,7 @@ void main(void) {
159160
// void* heap_page = allocate_pages(64 MiB / PAGE_SIZE);
160161
// mm_init(heap_page, 64 MiB);
161162

162-
struct memory_context* memory = (struct memory_context*)kmalloc(sizeof(struct memory_context));
163+
limine_memory_ctx = (struct memory_context*)kmalloc(sizeof(struct memory_context));
163164

164165
acpi_init();
165166

@@ -170,7 +171,7 @@ void main(void) {
170171

171172
RTL8139 = (struct rtl8139*) kmalloc(sizeof(struct rtl8139));
172173

173-
analyze_memory_map(memory, memory_map_request);
174+
analyze_memory_map(limine_memory_ctx, memory_map_request);
174175

175176
uintptr_t page1 = allocate_page();
176177
uintptr_t page2 = allocate_page();
@@ -196,10 +197,10 @@ void main(void) {
196197
printf("Display Resolution: %dx%d (%d) pixels. Pitch: %d", framebuffer->width, framebuffer->height, framebuffer->width*framebuffer->height, framebuffer->pitch);
197198

198199
info("Memory Values begin! ===", __FILE__);
199-
display_memory_formatted(memory);
200+
display_memory_formatted(limine_memory_ctx);
200201
info(reset_color "Memory values end! =====", __FILE__);
201202

202-
if(memory->bad != 0){
203+
if(limine_memory_ctx->bad != 0){
203204
warn("Bad blocks of memory found, it is recommended to replace your RAM.", __FILE__);
204205
}
205206

source/kernel/C/shell/commands/cat.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ int cmd_cat(int argc, char** argv)
4949
printfnoln("%c", buf[j]);
5050
}
5151

52+
if(j != 0)
53+
if(buf[j-1] != '\n')
54+
print("\n");
55+
5256
vfs_close(&file);
5357
}
54-
55-
if(j != 0)
56-
if(buf[j-1] != '\n')
57-
print("\n");
5858

5959
return 0;
6060
}

0 commit comments

Comments
 (0)