Skip to content

Commit 8e2feae

Browse files
author
tthomps
committed
Add CloseApp() function and system call for closing one app from another.
Support closing an application from the GUI. Closing an application's window closes the app.
1 parent 30aeb9f commit 8e2feae

File tree

12 files changed

+135
-3
lines changed

12 files changed

+135
-3
lines changed

MyOS_1/Build_Number.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
// Define a build number. This number will be incremented by a simple console program called by a post-build event
44
// It's put here in its own file to keep the other program from messing up the source
55
#define \
6-
BUILD_NUMBER 6438
6+
BUILD_NUMBER 6456

MyOS_1/Interrupts/IDT.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ void IDT_Init(void)
100100
// Set handler for launching an app
101101
Set_IDT_Entry((unsigned long)launch_app_interrupt_handler, SYSCALL_LAUNCH_APP);
102102

103+
// Set handler for closing an app
104+
Set_IDT_Entry((unsigned long)close_app_interrupt_handler, SYSCALL_CLOSE_APP);
105+
103106
/* fill the IDT descriptor */
104107
IDT_ptr.base = (uint32_t)IDT;
105108
IDT_ptr.size = (sizeof(IDT_ENTRY) * 256);

MyOS_1/Interrupts/Interrupts.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,33 @@ void _declspec(naked) irq9_shared_interrupt_handler(void)
9797
}
9898
}
9999

100+
void _declspec(naked) close_app_interrupt_handler(int eflags, int cs, uint32_t PID)
101+
{
102+
(void)eflags, (void)cs;
103+
104+
__asm
105+
{
106+
// prologue
107+
push ebp
108+
mov ebp, esp
109+
110+
// disable interrupts
111+
cli
112+
}
113+
114+
++interrupts_fired;
115+
116+
CloseApp(PID);
117+
118+
__asm
119+
{
120+
// epilogue
121+
pop ebp
122+
123+
iretd
124+
}
125+
}
126+
100127
void _declspec(naked) default_exception_handler(void)
101128
{
102129
//_asm pushad;

MyOS_1/Interrupts/Interrupts.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
extern unsigned long interrupts_fired;
1515

16+
void close_app_interrupt_handler(int eflags, int cs, uint32_t PID);
17+
1618
void default_exception_handler(void);
1719

1820
void exit_interrupt_handler(int eflags, int cs);

MyOS_1/Interrupts/System_Calls.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
#include "../Drivers/Keyboard.h"
1010

1111

12+
void SystemCallCloseApp(uint32_t PID)
13+
{
14+
const int pointerSize = sizeof(uint32_t);
15+
16+
__asm
17+
{
18+
push[PID] // push arguments onto stack
19+
int SYSCALL_CLOSE_APP // call close_app_interrupt_handler(PID)
20+
add esp, pointerSize // restore value of stack pointer
21+
}
22+
}
23+
1224
void SystemCallExit(int returnCode)
1325
{
1426
// returnCode is ignored for now

MyOS_1/Interrupts/System_Calls.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ extern "C" {
3131
#define SYSCALL_HIDE_SHELL_DISPLAY 239
3232
#define SYSCALL_REGISTER_GUI_CALLBACK 238
3333
#define SYSCALL_LAUNCH_APP 237
34+
#define SYSCALL_CLOSE_APP 236
35+
36+
void SystemCallCloseApp(uint32_t PID);
37+
#define closeApp SystemCallCloseApp
3438

3539
void SystemCallExit(int returnCode);
3640
#define exit SystemCallExit

MyOS_1/Tasks/Context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ extern uint32_t nextPID;
5959

6060
extern bool multiEnable; // TEMPTEMP
6161

62+
void CloseApp(uint32_t PID);
63+
6264
void DispatchNewTask(uint32_t programStart, PAGE_DIRECTORY_ENTRY *newPageDirectory, uint32_t stackSize, const char *imageName, bool exclusive);
6365

6466
void ExitApp();

MyOS_1/Tasks/Dispatcher.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../paging.h"
55
#include "../Networking/TFTP.h"
66
#include "../Executables/PE32.h"
7+
#include "../Console_Serial.h"
78

89
PROCESS_CONTROL_BLOCK tasks[64] = { 0 };
910
READY_QUEUE_ENTRY *readyQueueHead = NULL;
@@ -15,6 +16,78 @@ uint32_t nextPID = 1000;
1516
// This must be global so we can access it while playing with the stack in ways the compiler can't predict
1617
uint32_t stackPtr;
1718

19+
// TODO: rewrite when we have waiting queues
20+
// Interrupts are disabled by interrupt handler for system call.
21+
void CloseApp(uint32_t PID)
22+
{
23+
// If this task is running, let ExitApp() close it
24+
if (tasks[currentTask].PID == PID)
25+
ExitApp();
26+
27+
serial_printf("closing %d", PID);
28+
29+
// Find the index of the app that will be closed
30+
int taskIndex;
31+
for (taskIndex = 0; taskIndex < MAX_TASKS; ++taskIndex)
32+
{
33+
if (tasks[taskIndex].PID == PID)
34+
break;
35+
}
36+
if (taskIndex >= MAX_TASKS)
37+
{
38+
kprintf("ERROR! CloseApp() called with invalid PID, %d!", PID);
39+
return;
40+
}
41+
42+
serial_printf(" - %s\n", tasks[taskIndex].imageName);
43+
44+
// Has the task been closed already?
45+
if (!tasks[taskIndex].inUse)
46+
return;
47+
48+
if (!readyQueueHead)
49+
{
50+
kprintf("Attempted to close a task with an empty ready queue!\n");
51+
return;
52+
}
53+
54+
// Find the ready queue entry associated with the task (and the one that preceeds it)
55+
READY_QUEUE_ENTRY *pPreviousEntry = NULL;
56+
READY_QUEUE_ENTRY *pCurrent = readyQueueHead;
57+
58+
while (pCurrent && pCurrent->taskIndex != taskIndex)
59+
{
60+
pPreviousEntry = pCurrent;
61+
pCurrent = pCurrent->nextEntry;
62+
}
63+
64+
if (!pCurrent)
65+
{
66+
kprintf("ERROR! CloseApp() called with PID %d, but couldn't find it in the queue!\n", PID);
67+
serial_printf("ERROR! CloseApp() called with PID %d, but couldn't find it in the queue\n", PID);
68+
return;
69+
}
70+
71+
// Was the app to close listed in the first entry?
72+
if (!pPreviousEntry)
73+
{
74+
dbg_release(readyQueueHead);
75+
readyQueueHead = pCurrent;
76+
}
77+
else
78+
{
79+
// Remove the ready queue entry and delete it
80+
pPreviousEntry->nextEntry = pCurrent->nextEntry;
81+
dbg_release(pCurrent);
82+
}
83+
84+
85+
// TODO: Free all memory associated with the app, including its stack
86+
87+
// Mark the index as being free to use
88+
tasks[taskIndex].inUse = false;
89+
}
90+
1891
void DispatchNewTask(uint32_t programStart, PAGE_DIRECTORY_ENTRY *newPageDirectory, uint32_t stackSize, const char *imageName, bool exclusive)
1992
{
2093
// disable interrupts

MyOS_GUI_Shell/GUI_Kernel_Shell.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ extern "C" {
1111
#define \
1212
GUI_BASE_ADDRESS 0xC00000
1313

14+
#define WINDOW_ID_IS_NOT_PID 0x80000000 /* Window ID's with this bit set aren't associated with an application */
15+
1416
// Called by the kernel to send messages to the GUI
1517
void GUI_Kernel_Callback(uint32_t PID, uint32_t messageType, void *pData);
1618

MyOS_GUI_Shell/GUI_Window.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class GUI_Window : public GUI_Object
7979

8080
void UpdateCursor();
8181

82-
8382
SDL_Surface *pSurface;
8483
GUI_Control *pClickedControl;
8584
char windowName[MAX_WINDOW_NAME_LENGTH];

0 commit comments

Comments
 (0)