Skip to content

Commit 34f4acc

Browse files
committed
update shell, add free command, improve libc
1 parent 0867ca0 commit 34f4acc

File tree

8 files changed

+78
-46
lines changed

8 files changed

+78
-46
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
commit 0867ca069cf902e68c0ef2e5e605c32d2d0cabcc
2+
Author: Alexeev Bronislav <alexeev.dev@mail.ru>
3+
Date: Fri Aug 22 22:24:39 2025 +0700
4+
5+
improve info command, improve memory system
6+
17
commit 0176c2367607690e3198f7dccac0a1d26521e82d
28
Author: Alexeev Bronislav <alexeev.dev@mail.ru>
39
Date: Fri Aug 22 22:03:20 2025 +0700

src/kernel/kernel/kernel.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,18 @@ char** get_args(char *input) {
5656
void user_input(char *input) {
5757
// Массив структур команд, состоящий из самой команды, подсказки и указателя до void-функции
5858
struct { char *text, *hint; void (*command)(char**); } commands[] = {
59-
// Команда Подсказка для команды Указатель до функции
59+
// Команда Подсказка для команды Указатель до функции
6060
{.text="END", .hint="HALT CPU", .command=&halt_cpu},
6161
{.text="CLEAR", .hint="Clear screen", .command=&clear_screen_command},
6262
{.text="AMALLOC", .hint="Kernel Arena Malloc. Usage: AMALLOC <size>", .command=&arena_malloc_command_shell},
6363
{.text="QEMUSHUTDOWN", .hint="Shutdown QEMU", .command=&shutdown_qemu},
6464
{.text="INFO", .hint="Get info", .command=&info_command_shell},
6565
{.text="FREEMEMADDR", .hint="Get free mem addr", .command=&print_freememaddr},
6666
{.text="TESTMEM", .hint="Test memory", .command=&test_mem_command},
67-
{.text="KMEMDUMP", .hint="Dump memory", .command=&mem_dump},
68-
{.text="KMALLOC", .hint="Alloc memory. Usage: KMALLOC <size>", .command=&kmalloc_command}
67+
{.text="MEMDUMP", .hint="Dump memory", .command=&mem_dump},
68+
{.text="MALLOC", .hint="Alloc memory. Usage: MALLOC <size>", .command=&kmalloc_command},
69+
{.text="FREE", .hint="Free memory. Usage: FREE <address>", .command=&free_command},
70+
{.text="ECHO", .hint="Echo an text", .command=&echo_command}
6971
};
7072

7173
int executed = 0;

src/kernel/kernel/utils.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,32 @@ void mem_dump(char** args) {
7373
kmemdump();
7474
}
7575

76+
void echo_command(char **args) {
77+
for (int i = 0; args[i] != NULL; i++) {
78+
kprintf("%s ", args[i]);
79+
}
80+
kprint("\n");
81+
}
82+
83+
void free_command(char **args) {
84+
if (!args[0]) {
85+
kprint("FREE usage: FREE <hex_address>\n");
86+
return;
87+
}
88+
89+
char *addr_str = args[0];
90+
if (addr_str[0] == '0' && addr_str[1] == 'x') {
91+
addr_str += 2;
92+
}
93+
94+
u32 addr = hex_strtoint(addr_str);
95+
kfree((void*)addr);
96+
kprintf("Freed memory at %x\n", addr);
97+
}
98+
7699
void kmalloc_command(char** args) {
77100
if (args[0] == NULL) {
78-
kprint("KMALLOC usage: KMALLOC <bytes>");
101+
kprint("MALLOC usage: MALLOC <bytes>");
79102
return;
80103
}
81104

src/kernel/kernel/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ void test_mem_command(char** args);
1212
void mem_dump(char** args);
1313
void clear_screen_command(char** args);
1414
void kmalloc_command(char** args);
15+
void echo_command(char** args);
16+
void free_command(char **args);
1517

1618
#endif

src/kernel/libc/mem.c

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ void heap_init() {
4343
kprint("\n");
4444
}
4545

46+
// TODO: Paging is not implemented
47+
void *get_physaddr(void *virtualaddr) {
48+
unsigned long pdindex = (unsigned long)virtualaddr >> 22;
49+
unsigned long ptindex = (unsigned long)virtualaddr >> 12 & 0x03FF;
50+
51+
unsigned long *pd = (unsigned long *)0xFFFFF000;
52+
// Here you need to check whether the PD entry is present.
53+
54+
unsigned long *pt = ((unsigned long *)0xFFC00000) + (0x400 * pdindex);
55+
// Here you need to check whether the PT entry is present.
56+
57+
return (void *)((pt[ptindex] & ~0xFFF) + ((unsigned long)virtualaddr & 0xFFF));
58+
}
59+
4660
void* kmalloc_new(u32 size) {
4761
// Выравниваем размер до границы BLOCK_SIZE
4862
if (size % BLOCK_SIZE != 0) {
@@ -76,13 +90,13 @@ void* kmalloc_new(u32 size) {
7690
}
7791

7892
kprint("No free blocks found\n");
79-
return NULL; // Не нашли свободного блока
93+
return NULL;
8094
}
8195

8296
void kfree(void* ptr) {
8397
if (!ptr) return;
8498

85-
// Получаем указатель на заголовок блока
99+
// получаем указатель на заголовок блока
86100
mem_block_t* block = (mem_block_t*)((u32)ptr - sizeof(mem_block_t));
87101

88102
if (block->is_free) { // блок уже освобожден
@@ -92,13 +106,14 @@ void kfree(void* ptr) {
92106

93107
block->is_free = 1;
94108

95-
// Попробуем объединить с соседними свободными блоками
109+
/* попробуем объединить с соседними свободными блоками*/
96110
mem_block_t* current = free_blocks;
97111
while (current) {
98112
if (current->is_free && current->next && current->next->is_free) {
99-
// Объединяем текущий блок со следующим
113+
// соединим текущий блок со следующим
100114
current->size += sizeof(mem_block_t) + current->next->size;
101115
current->next = current->next->next;
116+
free_mem_addr -= current->size;
102117
}
103118
current = current->next;
104119
}
@@ -112,8 +127,6 @@ meminfo_t get_meminfo() {
112127
u32 total_free = 0;
113128
u32 block_count = 0;
114129

115-
kprint("Memory dump:\n");
116-
117130
while (current) {
118131
block_count++;
119132

@@ -137,46 +150,19 @@ meminfo_t get_meminfo() {
137150
return meminfo;
138151
}
139152

140-
void kmemdump() { // дамп памяти
141-
mem_block_t* current = free_blocks;
142-
u32 total_used = 0;
143-
u32 total_free = 0;
144-
u32 block_count = 0;
153+
void kmemdump() {
154+
meminfo_t info = get_meminfo();
155+
mem_block_t* current = info.free_blocks;
156+
u32 counter = 0;
145157

146-
kprint("Memory dump:\n");
158+
kprintf("Heap: %x - %x (%d bytes)\n", info.heap_start, info.heap_start + info.heap_size, info.heap_size);
159+
kprintf("Block size: %d bytes\n", info.block_size);
160+
kprintf("Total: USED=%d bytes, FREE=%d bytes, in %d blocks\n\n", info.total_used, info.total_free, info.block_count);
147161

148162
while (current) {
149-
kprint("Block ");
150-
char buf[32];
151-
int_to_ascii(block_count++, buf);
152-
kprint(buf);
153-
kprint(": Addr=");
154-
hex_to_ascii((u32)current, buf);
155-
kprint(buf);
156-
kprint(", Size=");
157-
int_to_ascii(current->size, buf);
158-
kprint(buf);
159-
kprint(", ");
160-
kprint(current->is_free ? "FREE" : "USED");
161-
kprint("\n");
162-
163-
if (current->is_free) {
164-
total_free += current->size;
165-
} else {
166-
total_used += current->size;
167-
}
168-
163+
kprintf("Block %d: %x, Size=%d, %s\n", counter++, (u32)current, current->size, current->is_free ? "FREE" : "USED");
169164
current = current->next;
170165
}
171-
172-
kprint("Total: USED=");
173-
char buf2[32];
174-
int_to_ascii(total_used, buf2);
175-
kprint(buf2);
176-
kprint(", FREE=");
177-
int_to_ascii(total_free, buf2);
178-
kprint(buf2);
179-
kprint("\n");
180166
}
181167

182168
// LEGACY Arena

src/kernel/libc/mem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ typedef struct meminfo {
2828
u32 total_used;
2929
u32 total_free;
3030
u32 block_count;
31-
3231
} meminfo_t;
3332

33+
void *get_physaddr(void *virtualaddr);
3434
meminfo_t get_meminfo();
3535
void memory_copy(u8 *source, u8 *dest, int nbytes);
3636
void memory_set(u8 *dest, u8 val, u32 len);

src/kernel/libc/string.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ void strcpy(char *dest, char *src) {
5959
*dest = '\0';
6060
}
6161

62+
int hex_strtoint(char *str) {
63+
int result = 0;
64+
while (*str) {
65+
result *= 16;
66+
if (*str >= '0' && *str <= '9') result += *str - '0';
67+
else if (*str >= 'a' && *str <= 'f') result += *str - 'a' + 10;
68+
else if (*str >= 'A' && *str <= 'F') result += *str - 'A' + 10;
69+
str++;
70+
}
71+
return result;
72+
}
73+
6274
/* K&R */
6375
void reverse(char s[]) {
6476
int c, i, j;

src/kernel/libc/string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ int strlen(char s[]);
99
void backspace(char s[]);
1010
void append(char s[], char n);
1111
int strcmp(char s1[], char s2[]);
12+
int hex_strtoint(char *str);
1213
void strcpy(char *dest, char *src);
1314
unsigned int is_delim(char c, char *delim);
1415
char *strtok(char *src_str, char *delim);

0 commit comments

Comments
 (0)