45ec3c1a7b
Also free userspace allocated memory
79 lines
2.1 KiB
C
79 lines
2.1 KiB
C
#include "libc.h"
|
|
#include "stdarg.h"
|
|
#include "tiny.h"
|
|
|
|
int func_help()
|
|
{
|
|
printf("\nAvailable Commands:\n");
|
|
printf(" help: this help\n");
|
|
printf(" suicide: userspace self kill\n");
|
|
printf(" syscall5: a syscall with parameters over the stack \n");
|
|
printf(" alloc: test allocation\n");
|
|
printf(" tiny: a tiny C-like interpreter\n");
|
|
return 0;
|
|
}
|
|
|
|
int func_suicide()
|
|
{
|
|
printf("User is about to suicide\n");
|
|
int *yolo = 0;
|
|
*yolo = 1;
|
|
return 0;
|
|
}
|
|
|
|
char *initialHeap = 0;
|
|
int func_alloc()
|
|
{
|
|
|
|
if (initialHeap == 0) {
|
|
initialHeap = sbrk(0);
|
|
}
|
|
printf("Testing allocation\n");
|
|
int allocSize = 4096 * 2;
|
|
char *currentHeap = sbrk(0);
|
|
if (currentHeap - initialHeap < allocSize) {
|
|
brk(initialHeap + allocSize);
|
|
}
|
|
int *allocatedData = (int *)initialHeap;
|
|
for (unsigned int i = 0; i < allocSize / sizeof(int); i++) {
|
|
allocatedData[i] = i;
|
|
}
|
|
printf("Success\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
(void)argc;
|
|
(void)argv;
|
|
char buf[64];
|
|
printf("Shell starting... type \"help\" for help\n");
|
|
while (1) {
|
|
printf(">");
|
|
if (readline(buf, sizeof(buf)))
|
|
continue;
|
|
if (strcmp(buf, "help") == 0) {
|
|
func_help();
|
|
continue;
|
|
}
|
|
if (strcmp(buf, "suicide") == 0) {
|
|
func_suicide();
|
|
continue;
|
|
}
|
|
if (strcmp(buf, "syscall5") == 0) {
|
|
testSycall5(1, 2, 3, 4, 5);
|
|
continue;
|
|
}
|
|
if (strcmp(buf, "alloc") == 0) {
|
|
func_alloc();
|
|
continue;
|
|
}
|
|
if (strcmp(buf, "tiny") == 0) {
|
|
func_tiny();
|
|
continue;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|