Skip to content

Commit 4d0c159

Browse files
author
tthomps
committed
Add LaunchApp() function and launchApp System call for launching a new process from the GUI or command line. Replace relevant sections of console commands with LaunchApp() calls.
Add string representation of an edit control's contents. Allow Run Window to launch an application. Copy image name to GUI_NEW_CONSOLE_APP_DATA and replace pointer to image name with a buffer to hold the name (prevents a crash). Update a few functions to be "const-correct."
1 parent 57fef23 commit 4d0c159

22 files changed

+193
-112
lines changed

MyOS_1/Console_Shell.c

Lines changed: 9 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "Drivers/Virtio_GPU.h"
3030
#include "myos_io.h"
3131
#include "Drivers\Keyboard.h"
32+
#include "../MyOS_GUI_Shell/GUI_Kernel_Shell.h"
3233

3334
int inputPosition = 0;
3435
#define COMMAND_HISTORY_SIZE 10
@@ -919,54 +920,14 @@ void Shell_Process_command(void)
919920
return;
920921
}
921922

922-
// TEMPTEMP we've hardcoded some memory starting at 0x800000. This was identity mapped when paging was enabled.
923-
uint8_t *exeBuffer = (uint8_t*)0x800000;
923+
// TEMPTEMP apps should start at 0x80 0000
924+
uint32_t exeBuffer = 0x800000;
924925

925926
// reset keyboard input buffer
926927
keyReadIndex = keyWriteIndex = 0;
927-
928-
// Get the size of the executable file
929-
uint32_t fileSize;
930-
if (!TFTP_GetFileSize(tftpServerIP, subCommand, &fileSize))
931-
{
932-
terminal_writestring("Failed to determine size of ");
933-
terminal_writestring(subCommand);
934-
terminal_newline();
935-
return;
936-
}
937-
938-
// Allocate a buffer for the executable file
939-
uint8_t *peBuffer = malloc(fileSize);
940-
if (!peBuffer)
941-
{
942-
terminal_writestring("Not enough memory to open executable file\n");
943-
return;
944-
}
945-
946-
//uint32_t peFileSize;
947-
948-
if (debugLevel)
949-
terminal_dumpHex(peBuffer, 32);
950-
951-
// Download the executable
952-
if (!TFTP_GetFile(tftpServerIP, subCommand, peBuffer, fileSize, NULL))
953-
{
954-
terminal_writestring("Error reading ");
955-
terminal_writestring(subCommand);
956-
terminal_writestring(" from server!\n");
957-
return;
958-
}
959-
960-
if (debugLevel)
961-
terminal_dumpHex(peBuffer, 32);
962-
963-
// Run the executable
964-
if (!loadAndRunPE(exeBuffer, (DOS_Header*)peBuffer, subCommand, exclusive))
965-
terminal_writestring("Error running executable\n");
966-
967-
// Free the memory for the pe
968-
free(peBuffer);
969-
928+
929+
LaunchApp(subCommand, exclusive, exeBuffer);
930+
970931
terminal_resume();
971932

972933
if (debugLevel)
@@ -1027,56 +988,12 @@ void Shell_Process_command(void)
1027988
return;
1028989
}
1029990

1030-
// TEMPTEMP we've hardcoded some memory starting at 0xC00000. This was identity mapped when paging was enabled.
1031-
uint8_t *exeBuffer = (uint8_t*)0xC00000;
1032-
1033991
// reset keyboard input buffer
1034992
keyReadIndex = keyWriteIndex = 0;
1035993

1036-
// Get the size of the executable file
1037-
uint32_t fileSize;
1038-
if (!TFTP_GetFileSize(tftpServerIP, subCommand, &fileSize))
1039-
{
1040-
terminal_writestring("Failed to determine size of ");
1041-
terminal_writestring(subCommand);
1042-
terminal_newline();
1043-
return;
1044-
}
1045-
1046-
// Allocate a buffer for the executable file
1047-
uint8_t *peBuffer = malloc(fileSize);
1048-
if (!peBuffer)
1049-
{
1050-
terminal_writestring("Not enough memory to open executable file\n");
1051-
return;
1052-
}
1053-
1054-
//uint32_t peFileSize;
1055-
1056-
if (debugLevel)
1057-
terminal_dumpHex(peBuffer, 32);
1058-
1059-
// Download the executable
1060-
if (!TFTP_GetFile(tftpServerIP, subCommand, peBuffer, fileSize, NULL))
1061-
{
1062-
terminal_writestring("Error reading ");
1063-
terminal_writestring(subCommand);
1064-
terminal_writestring(" from server!\n");
1065-
return;
1066-
}
1067-
1068-
if (debugLevel)
1069-
terminal_dumpHex(peBuffer, 32);
1070-
1071-
1072-
// Run the executable
1073-
if (!loadAndRunPE(exeBuffer, (DOS_Header*)peBuffer, subCommand, exclusive))
1074-
terminal_writestring("Error running executable\n");
1075-
1076-
1077-
// Free the memory for the pe
1078-
free(peBuffer);
1079-
994+
// TEMPTEMP we've hardcoded some memory starting at 0xC00000. This was identity mapped when paging was enabled.
995+
LaunchApp(subCommand, exclusive, GUI_BASE_ADDRESS);
996+
1080997
terminal_resume();
1081998

1082999
if (debugLevel)

MyOS_1/Executables/PE32.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ bool loadAndRunPE(uint8_t *executableDestination, DOS_Header *mzAddress, const c
120120
newPageDirectory = pageDir;
121121

122122
// TEMPTEMP - zero out 5 0x1000 sections of memory (tailored to TestApp1.exe)
123-
memset(physicalLocation, 0, 0x5000);
123+
memset((void*)physicalLocation, 0, 0x5000);
124124

125125
// Get the address of the first section
126126
IMAGE_SECTION_HEADER *sectionHeader = (IMAGE_SECTION_HEADER*)((uint32_t)directory + sizeof(IMAGE_DATA_DIRECTORY));
@@ -177,7 +177,6 @@ bool loadAndRunPE(uint8_t *executableDestination, DOS_Header *mzAddress, const c
177177

178178
if (multiEnable)
179179
{
180-
// TEMP: use kernel page directory
181180
DispatchNewTask((uint32_t)entryPoint, newPageDirectory, 0x20000, imageName, exclusive);
182181
}
183182
else

MyOS_1/GUI_Kernel.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "GUI_Messages.h"
44
#include "Tasks/Context.h"
55
#include "printf.h"
6+
#include "misc.h"
67

78
GUI_CALLBACK guiCallback = NULL;
89

@@ -37,7 +38,9 @@ void GUI_CreateConsoleWindowForApp(uint32_t taskNumber)
3738
{
3839
// Construct the message data
3940
GUI_NEW_CONSOLE_APP_DATA data;
40-
data.appName = tasks[taskNumber].imageName;
41+
//data.appName = tasks[taskNumber].imageName;
42+
memset(data.appName, 0, MAX_IMAGE_NAME_LENGTH);
43+
strncpy(data.appName, tasks[taskNumber].imageName, MAX_IMAGE_NAME_LENGTH - 1);
4144

4245
(*guiCallback)(tasks[taskNumber].PID, GUI_MSG_NEW_CONSOLE_APP, &data);
4346
}

MyOS_1/GUI_Messages.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
// END OF MESSAGES
88

99
#define MAX_GUI_CONSOLE_STRING_LENGTH 512
10+
#define MAX_IMAGE_NAME_LENGTH 32
1011

1112
// DATA TYPES USED BY MESSAGES
1213

1314
// Data type used by GUI_MSG_NEW_CONSOLE_APP messages
1415
typedef struct GUI_NEW_CONSOLE_APP_DATA
1516
{
16-
const char *appName;
17+
char appName[MAX_IMAGE_NAME_LENGTH];
1718
// TODO: We may as well send the terminal contents over in the same message
1819
} GUI_NEW_CONSOLE_APP_DATA;
1920

MyOS_1/Interrupts/IDT.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ void IDT_Init(void)
9797
// Set handler for the GUI telling the kernel how to send the GUI messages
9898
Set_IDT_Entry((unsigned long)gui_register_callback_interrupt_handler, SYSCALL_REGISTER_GUI_CALLBACK);
9999

100+
// Set handler for launching an app
101+
Set_IDT_Entry((unsigned long)launch_app_interrupt_handler, SYSCALL_LAUNCH_APP);
102+
100103
/* fill the IDT descriptor */
101104
IDT_ptr.base = (uint32_t)IDT;
102105
IDT_ptr.size = (sizeof(IDT_ENTRY) * 256);

MyOS_1/Interrupts/Interrupts.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,32 @@ void _declspec(naked) hide_shell_display_interrupt_handler(int eflags, int cs)
512512
}
513513
}
514514

515+
void _declspec(naked) launch_app_interrupt_handler(int eflags, int cs, const char *appFilename, bool exclusive, bool *pRetVal)
516+
{
517+
// supress warning about unused parameters
518+
(void)eflags, (void)cs;
519+
520+
// prologue
521+
_asm
522+
{
523+
push ebp
524+
mov ebp, esp
525+
526+
//pushad
527+
}
528+
529+
*pRetVal = LaunchApp(appFilename, exclusive, 0x800000);
530+
531+
// epilogue
532+
_asm
533+
{
534+
//popad
535+
536+
pop ebp
537+
iretd
538+
}
539+
}
540+
515541
void _declspec(naked) page_allocator_interrupt_handler(int eflags, int cs, unsigned int pages, unsigned int *pPagesAllocated, uint32_t *pRetVal)
516542
{
517543
// supress warning about unused parameters

MyOS_1/Interrupts/Interrupts.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ void Interrupts_Init();
4949

5050
void keyboard_interrupt_handler(void);
5151

52+
void launch_app_interrupt_handler(int eflags, int cs, const char *appFilename, bool exclusive, bool *pRetVal);
53+
5254
void page_allocator_interrupt_handler(int eflags, int cs, unsigned int pages, unsigned int *pPagesAllocated, uint32_t *pRetVal);
5355

5456
void page_fault_handler(void);

MyOS_1/Interrupts/System_Calls.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,25 @@ long int SystemCallFTell(FILE * stream)
124124
return retVal;
125125
}
126126

127+
bool SystemCallLaunchApp(const char * appFileName, bool exclusive)
128+
{
129+
bool retVal;
130+
bool *pRetVal = &retVal;
131+
int exclusiveInt = exclusive;
132+
const int pointerSize = sizeof(const char *) + sizeof(int) + sizeof(bool *);
133+
134+
__asm
135+
{
136+
push[pRetVal] // push arguments onto stack
137+
push[exclusiveInt]
138+
push[appFileName]
139+
int SYSCALL_LAUNCH_APP // call launch_app_interrupt_handler(appFilename, exclusiveInt, pRetVal)
140+
add esp, pointerSize // restore value of stack pointer
141+
}
142+
143+
return retVal;
144+
}
145+
127146
// Get graphics info
128147
void SystemCallGetGraphicsInfo(bool *graphicsMode, int *width, int *height)
129148
{

MyOS_1/Interrupts/System_Calls.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" {
3030
#define SYSCALL_GET_MOUSE_STATE 240
3131
#define SYSCALL_HIDE_SHELL_DISPLAY 239
3232
#define SYSCALL_REGISTER_GUI_CALLBACK 238
33+
#define SYSCALL_LAUNCH_APP 237
3334

3435
void SystemCallExit();
3536
#define exit SystemCallExit
@@ -49,6 +50,9 @@ int SystemCallFSeek(FILE * stream, long int offset, int origin);
4950
long int SystemCallFTell(FILE * stream);
5051
#define ftell SystemCallFTell
5152

53+
bool SystemCallLaunchApp(const char *appFileName, bool exclusive);
54+
#define launchApp SystemCallLaunchApp
55+
5256
void SystemCallPageAllocator(unsigned int pages, unsigned int *pPagesAllocated, uint32_t *pRreturnVal);
5357
#define pageAllocator SystemCallPageAllocator
5458

MyOS_1/Networking/TFTP.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ bool TFTP_TransactionComplete()
3131
// TODO: Implement timeout, error-checking
3232
// TODO: Fix hanging if an invalid serverIP is given
3333
// actualFileSize can be NULL if the caller doesn't care about the size
34-
bool TFTP_GetFile(uint32_t serverIP, char *filename, uint8_t *destinationBuffer, uint32_t maxFileSize, uint32_t *actualFileSize)
34+
bool TFTP_GetFile(uint32_t serverIP, const char *filename, uint8_t *destinationBuffer, uint32_t maxFileSize, uint32_t *actualFileSize)
3535
{
3636
if(actualFileSize)
3737
*actualFileSize = 0;
@@ -77,7 +77,7 @@ bool TFTP_GetFile(uint32_t serverIP, char *filename, uint8_t *destinationBuffer,
7777

7878
// TODO: Same as above
7979
// Retrieves the size of a file. This can take a while for large files, because we actually have to retrieve the entire file to know its size.
80-
bool TFTP_GetFileSize(uint32_t serverIP, char *filename, uint32_t *pActualFileSize)
80+
bool TFTP_GetFileSize(uint32_t serverIP, const char *filename, uint32_t *pActualFileSize)
8181
{
8282
// Ensure pActualFileSize is a valid pointer
8383
if (!pActualFileSize)
@@ -111,7 +111,7 @@ bool TFTP_GetFileSize(uint32_t serverIP, char *filename, uint32_t *pActualFileSi
111111
}
112112

113113
// source port is used to keep track of different transactions
114-
uint16_t TFTP_RequestFile(uint32_t serverIP, char *filename, char *transferMode, uint8_t *sourceMAC)
114+
uint16_t TFTP_RequestFile(uint32_t serverIP, const char *filename, const char *transferMode, uint8_t *sourceMAC)
115115
{
116116
TFTP_RequestHeader *tftpData;
117117
size_t filenameLength = strlen(filename) + 1; // length of filename plus null terminator
@@ -148,7 +148,7 @@ uint16_t TFTP_RequestFile(uint32_t serverIP, char *filename, char *transferMode,
148148

149149
// source port is used to keep track of different transactions
150150
// TODO: Seems like Qemu and VirtualBox don't support writing files via TFTP so I can't finish this :(
151-
uint16_t TFTP_WriteFile(uint32_t serverIP, char *filename, char *transferMode, uint8_t *sourceMAC)
151+
uint16_t TFTP_WriteFile(uint32_t serverIP, const char *filename, const char *transferMode, uint8_t *sourceMAC)
152152
{
153153
TFTP_RequestHeader *tftpData;
154154
size_t filenameLength = strlen(filename) + 1; // length of filename plus null terminator

MyOS_1/Networking/TFTP.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ extern bool tftpHideErrors;
8484
extern uint32_t tftpServerIP;
8585

8686

87-
bool TFTP_GetFile(uint32_t serverIP, char *filename, uint8_t *destinationBuffer, uint32_t maxFileSize, uint32_t *actualFileSize);
87+
bool TFTP_GetFile(uint32_t serverIP, const char *filename, uint8_t *destinationBuffer, uint32_t maxFileSize, uint32_t *actualFileSize);
8888

89-
bool TFTP_GetFileSize(uint32_t serverIP, char *filename, uint32_t *pActualFileSize);
89+
bool TFTP_GetFileSize(uint32_t serverIP, const char *filename, uint32_t *pActualFileSize);
9090

91-
uint16_t TFTP_RequestFile(uint32_t serverIP, char *filename, char *transferMode, uint8_t *sourceMAC);
91+
uint16_t TFTP_RequestFile(uint32_t serverIP, const char *filename, const char *transferMode, uint8_t *sourceMAC);
9292

9393
void TFTP_ProcessPacket(TFTP_Header *packet, uint16_t sourcePort, uint16_t destinationPort, uint16_t packetLength, uint8_t *sourceMAC);
9494

95-
uint16_t TFTP_WriteFile(uint32_t serverIP, char *filename, char *transferMode, uint8_t *sourceMAC);
95+
uint16_t TFTP_WriteFile(uint32_t serverIP, const char *filename, const char *transferMode, uint8_t *sourceMAC);

MyOS_1/Tasks/Context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ extern bool multiEnable; // TEMPTEMP
6161

6262
void DispatchNewTask(uint32_t programStart, PAGE_DIRECTORY_ENTRY *newPageDirectory, uint32_t stackSize, const char *imageName, bool exclusive);
6363

64+
bool LaunchApp(const char *appName, int exclusive, uint32_t exeLocation);
65+
6466
void SwitchTask(uint32_t taskIndex);

0 commit comments

Comments
 (0)