os-k/kaleid/kernel/sh/testcmds.c

339 lines
9.8 KiB
C
Raw Permalink Normal View History

2019-11-28 13:02:47 +01:00
//----------------------------------------------------------------------------//
2020-09-27 17:33:48 +02:00
// OS on Kaleid //
2019-11-28 13:02:47 +01:00
// //
// Desc: Kernel shell //
// //
// //
2021-02-18 19:54:35 +01:00
// Copyright © 2018-2021 The OS/K Team //
2019-11-28 13:02:47 +01:00
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
2019-11-28 13:02:47 +01:00
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <vers.h>
2020-01-12 16:53:23 +01:00
#include <mm/paging.h>
2020-01-13 20:48:24 +01:00
#include <mm/palloc.h>
2020-01-12 16:53:23 +01:00
#include <mm/map.h>
2020-02-19 22:19:58 +01:00
#include <drivers/ata.h>
2019-11-28 13:02:47 +01:00
#include <io/vga.h>
#include <io/spkr.h>
#include <ke/time.h>
2020-02-19 22:19:58 +01:00
#include <libbuf.h>
2019-11-28 13:02:47 +01:00
#include <sh/shell.h>
#include <po/shtdwn.h>
static Command_t testcmdtable[];
error_t CmdTest(int argc, char **argv, char *cmdline)
{
ExecuteCommand(cmdline+5, testcmdtable);
KernLog ("\n");
return EOK;
}
error_t CmdArgs(int argc, char **argv, char *cmdline)
{
int i;
KernLog("cmdline: '%s'\nargc: %d\n", cmdline, argc);
for (i = 0; i < argc; i++) {
KernLog("argv[%d]: '%s'\n", i, argv[i]);
}
return EOK;
}
error_t CmdAtoi(int argc, char **argv, char *cmdline)
{
int i;
KernLog("cmdline: '%s'\nargc: %d\n", cmdline, argc);
for (i = 0; i < argc; i++) {
KernLog("argv[%d]: '%u'\n", i, atoi(argv[i]));
}
return EOK;
}
2019-11-28 13:02:47 +01:00
error_t CmdDumpATASect(int argc, char **argv, char *cmdline)
{
char sector[512] = {0};
2019-12-07 17:54:48 +01:00
int sectNumber = atoi(argv[1]);
int nb = 1; //atoi(argv[2]);
2019-11-28 13:02:47 +01:00
int x = 0;
int step = 16;
if (sectNumber <= 0 || sectNumber > 255) {
KernLog("Bad argument\n\n");
return EINVAL;
}
if (!nb) nb = 1;
KernLog("Sector begin: %d\n", (sectNumber - 1)*512);
IoReadATA(sector, nb, sectNumber);
while(x < 512*nb) {
KernLog("%C", shcol);
for (int i = 0; i < step; i++) {
KernLog("%02x ", (uchar)sector[i+x]);
}
KernLog(" %C ", VGA_COLOR_LIGHT_BLUE);
for (int i = 0; i < step; i++) {
if (isprint(sector[i+x]))
KernLog("%c",
sector[i+x]
);
else
KernLog("%c", 0);
}
KernLog("\n");
x += step;
}
KernLog("\n\n");
return EOK;
}
error_t CmdFloatDiv(int argc, char **argv, char *cmdline)
{
2019-12-07 17:54:48 +01:00
double a = (double)atoi(argv[1]);
double b = (double)atoi(argv[2]);
2019-11-28 13:02:47 +01:00
double number = a / b;
double number_ent = (double)(ulong)number;
double dec = number - number_ent;
double sub = 0.0;
char res[17]; // 10e-16 is the max precision
for(int i = 0; i < 16; i++) {
dec = (dec * 10.0) - (sub * 10.0);
sub = (double)(ulong)dec;
snprintf(&res[i], 17-i, "%d", (ulong)dec);
}
KernLog("%d / %d = %d.%s \n", (ulong)a, (ulong)b, (ulong)number_ent, res);
return EOK;
}
error_t CmdHelpTest(int argc, char **argv, char *cmdline)
{
uint i, count = 0;
Command_t *cmd;
if (argc == 1) {
KernLog("List of all test built-ins:\n");
for (cmd = testcmdtable; cmd->name != NULL; cmd++, count++) {
KernLog("\t%s", cmd->name);
for (i = strlen(cmd->name)/4; i<3; i++) {
KernLog("\t");
}
KernLog("%C%s%C\n", VGA_COLOR_DARK_GREY, cmd->help, shcol);
}
KernLog("End of list; %u commands total\n", count);
}
return EOK;
}
2020-01-09 20:58:57 +01:00
error_t CmdPageTranslateVirtToPhy(int argc, char **argv, char *cmdline)
{
void *address = (void*)atoul(argv[1]);
2020-01-10 12:51:23 +01:00
if (!(void*)strtoul(argv[1], NULL, 16)) {
KernLog("No argument : translating the userspace address\n");
address = (void *)0x80000000;
2020-01-09 20:58:57 +01:00
}
void *translation = MmTransVirtToPhyAddr(address);
KernLog("Translation of %p is %p\n", address, translation);
return EOK;
}
2020-01-10 22:41:25 +01:00
error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline)
{
void *address = (void*)strtoul(argv[1], NULL, 16);
/* if (!(void*)atoul(argv[1])) { */
/* address = (ulong *)0x80000000; */
/* } */
void *translation = MmTransPhyToVirtAddr(address);
KernLog("Translation of %p is %p\n", address, translation);
return EOK;
}
2020-01-09 23:01:00 +01:00
error_t CmdPageMap(int argc, char **argv, char *cmdline)
{
2020-01-10 12:51:23 +01:00
void *virtual = (void*)strtoul(argv[1], NULL, 16);
void *physical = (void*)strtoul(argv[2], NULL, 16);
2020-01-09 23:01:00 +01:00
MmMapPage(virtual, physical, PRESENT | READWRITE);
return EOK;
}
error_t CmdPageUnmap(int argc, char **argv, char *cmdline)
{
2020-01-10 12:51:23 +01:00
void *virtual = (void*)strtoul(argv[1], NULL, 16);
2020-01-09 23:01:00 +01:00
MmUnmapPage(virtual);
return EOK;
}
2020-01-21 01:08:28 +01:00
error_t CmdPageAlloc(int argc, char **argv, char *cmdline)
2020-01-09 20:58:57 +01:00
{
2020-01-23 17:38:51 +01:00
if (argc != 4) {
KernLog("Invalid arguments !\n");
return EINVAL;
}
size_t size = (size_t)atol(argv[1]);
void *virtaddr = (void *)strtol(argv[2], NULL, 16);
ulong flags = (ulong)atol(argv[3]);
KernLog("Allocating %u o...\n", size);
2020-01-23 17:38:51 +01:00
ulong id = MmAllocPageFrame(size, false);
KernLog("Allocated with id : %lu\n", id);
KernLog("Mapping pages id %d at %p (flags %#x)...\n", id, virtaddr, flags);
error_t err = MmMapPageFrame(id, virtaddr, flags);
if (err == EOK)
KernLog("Successful mapped.\n");
else
KernLog("Failed to map !\n");
return EOK;
}
error_t CmdPageFree(int argc, char **argv, char *cmdline)
{
if (argc != 2) {
KernLog("Invalid arguments !\n");
return EINVAL;
}
ulong id = (ulong)atol(argv[1]);
KernLog("Unmapping pages id %d...\n", id);
error_t err = MmUnmapPageFrame(id);
if (err == EOK)
KernLog("Success unmaping.\n");
else {
KernLog("Failed to unmap !\n");
return EFAILED;
}
KernLog("Freeing pages id %d...\n", id);
MmFreePageFrame(id);
return EOK;
2020-01-09 20:58:57 +01:00
}
2019-11-28 13:02:47 +01:00
error_t CmdPF(int argc, char **argv, char *cmdline)
{
2020-01-12 17:21:12 +01:00
register ulong *address = (ulong*)(ulong)strtoul(argv[1], NULL, 16);
2020-01-09 18:19:49 +01:00
2020-01-12 17:21:12 +01:00
KernLog("Test provoking a fault at %p\n", address);
2019-11-28 13:02:47 +01:00
2020-01-12 17:21:12 +01:00
KernLog("It contained %p\n", *address);
2019-11-28 13:02:47 +01:00
*address = 1;
2020-01-12 17:21:12 +01:00
KernLog("Now it contains %p\n", *address);
2019-11-28 13:02:47 +01:00
KernLog("No page fault : address was valid/present\n");
return EOK;
}
extern void pstest(void);
error_t CmdPS(int argc, char **argv, char *cmdline)
{
pstest();
return EOK;
}
2019-11-28 13:02:47 +01:00
error_t CmdShell(int argc, char **argv, char *cmdline)
{
ShStartShell();
return EOK;
}
error_t CmdStackOverflow(int argc, char **argv, char *cmdline)
{
CmdStackOverflow(0, 0, 0);
return EOK;
}
error_t CmdStackUnderflow(int argc, char **argv, char *cmdline)
{
for (int i = 0 ;; i++) {
asm volatile ("pop %rax\n");
}
return EOK;
}
error_t CmdTimerTest(int argc, char **argv, char *cmdline)
{
2019-12-07 17:54:48 +01:00
int delay = atoi(argv[1]);
2019-11-28 13:02:47 +01:00
Timer_t *timer = KeSetTimer(delay);
while(!(KeGetTimer(timer))) {
bprintf(BStdOut,".");
BStdOut->flusher(BStdOut);
}
KernLog("Finished !\n");
return EOK;
}
static Command_t testcmdtable[] =
{
{ "args", CmdArgs, "Print command line" },
{ "atoi", CmdAtoi, "Print command line atoised" },
2019-11-28 13:02:47 +01:00
{ "dmpsec", CmdDumpATASect, "Dump an ATA sector on screen" },
{ "help", CmdHelpTest, "Show this message" },
{ "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"},
2020-01-09 20:58:57 +01:00
{ "transvtp", CmdPageTranslateVirtToPhy, "Translate a virtual to"
" physical address (paging)"},
2020-01-10 19:25:33 +01:00
{ "transptv", CmdPageTranslatePhyToVirt, "Translate a physical to"
" virtual address (paging)"},
2020-01-09 23:01:00 +01:00
{ "pmap", CmdPageMap, "Map a page to given physical addr" },
{ "punmap", CmdPageUnmap, "Unmap a page" },
2020-01-23 17:38:51 +01:00
{ "palloc", CmdPageAlloc, "Alloc x B of pages at y address"
"with z flags"},
{ "pfree", CmdPageFree, "Free a page block of id x" },
2019-11-28 13:02:47 +01:00
{ "pf", CmdPF, "Provoke a PF. Usage: pfault <address>"},
{ "ps", CmdPS, "Scheduler algorithm test" },
2019-11-28 13:02:47 +01:00
{ "shell", CmdShell, "Start a new shell (nested)", },
{ "stkov", CmdStackOverflow, "Provoke a stack overflow" },
{ "stkun", CmdStackUnderflow, "Provoke a stack underflow" },
{ "timer", CmdTimerTest, "test timer of x ms" },
{ NULL, NULL, NULL }
};