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

324 lines
9.2 KiB
C
Raw Normal View History

2019-11-28 13:02:47 +01:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Kernel shell //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// 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>
#include <mm/mm.h>
#include <io/ata.h>
#include <io/vga.h>
#include <io/spkr.h>
#include <ke/time.h>
#include <lib/buf.h>
#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;
}
2020-01-07 00:44:39 +01:00
error_t CmdDumpMem(int argc, char **argv, char *cmdline)
{
char sector[1024] = {0};
2020-01-10 12:51:23 +01:00
char *address = (char*)strtoul(argv[1], NULL, 16);
2020-01-07 00:44:39 +01:00
int nb = 1; //atoi(argv[2]);
int x = 0;
int step = 16;
KernLog("Address begin: %p\n", address);
for (int i = 0; i < 1024*nb; i++) {
sector[i] = *address++;
}
while(x < 1024*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;
}
2019-11-28 13:02:47 +01:00
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-09 23:01:00 +01:00
enum
{
PRESENT = 1 << 0,
READWRITE = 1 << 1,
USERMODE = 1 << 2,
WRITETHR = 1 << 3,
CACHEDIS = 1 << 4,
ACCESSED = 1 << 5,
DIRTY = 1 << 6,
HUGE = 1 << 7,
NX = 1UL << 63
};
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-09 20:58:57 +01:00
error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline)
{
2020-01-10 12:51:23 +01:00
void *address = (void*)strtoul(argv[1], NULL, 16);
2020-01-09 20:58:57 +01:00
/* if (!(void*)atoul(argv[1])) { */
/* address = (ulong *)0x80000000; */
/* } */
void *translation = MmTransPhyToVirtAddr(address);
KernLog("Translation of %p is %p\n", address, translation);
return EOK;
}
2019-11-28 13:02:47 +01:00
error_t CmdPF(int argc, char **argv, char *cmdline)
{
2020-01-10 12:51:23 +01:00
ulong *address = (ulong*)(ulong)strtoul(argv[1], NULL, 16);
2020-01-09 18:19:49 +01:00
2019-11-28 13:02:47 +01:00
KernLog("Provoking Page Fault at %#x\n", address);
KernLog("It contained %#x\n", *address);
2019-11-28 13:02:47 +01:00
*address = 1;
KernLog("Now it contains %#x\n", *address);
2019-11-28 13:02:47 +01:00
KernLog("No page fault : address was valid/present\n");
return EOK;
}
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" },
2020-01-07 00:44:39 +01:00
{ "dmp", CmdDumpMem, "Dump 1MB of memory starting from addr"},
2019-11-28 13:02:47 +01:00
{ "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-09 23:01:00 +01:00
{ "pmap", CmdPageMap, "Map a page to given physical addr" },
{ "punmap", CmdPageUnmap, "Unmap a page" },
2019-11-28 13:02:47 +01:00
{ "pf", CmdPF, "Provoke a PF. Usage: pfault <address>"},
{ "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 }
};