Skip to content

Commit 8ce4074

Browse files
committed
Patch version v0.25.0
Make video drivers implement generic "libvideo" interface; let video drivers register devices with device manager and populate events via topic; use generic windowserver video output based on these events
1 parent da86d3b commit 8ce4074

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1112
-739
lines changed

applications/ahcidriver/src/ahcidriver.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ static uint8_t controllerIntrLine;
2929

3030
int main()
3131
{
32-
g_task_register_id(G_AHCI_DRIVER_IDENTIFIER);
32+
if(!g_task_register_name(G_AHCI_DRIVER_NAME))
33+
{
34+
klog("failed to register as %s", G_AHCI_DRIVER_NAME);
35+
return -1;
36+
}
3337

3438
if(!ahciDriverIdentifyController())
3539
{

applications/devicemanager/src/manager.cpp

+70-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2-
* *
2+
* *
33
* Ghost, a micro-kernel based operating system for the x86 architecture *
44
* Copyright (C) 2025, Max Schlüssel <[email protected]> *
55
* *
@@ -18,23 +18,31 @@
1818
* *
1919
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020

21+
#include "manager.hpp"
2122
#include <ghost.h>
2223
#include <libpci/driver.hpp>
24+
#include <libdevice/interface.hpp>
2325
#include <cstdio>
26+
#include <unordered_map>
2427

25-
// TODO:
26-
// This is the device manager that is responsible to keep track of devices in
27-
// the system, give each an ID and manage access to them. It also starts the
28-
// right drivers once it knows which devices exist.
28+
void _deviceManagerCheckPciDevices();
29+
void _deviceManagerAwaitCommands();
30+
void _deviceManagerHandleRegisterDevice(g_tid sender, g_message_transaction tx,
31+
g_device_manager_register_device_request* content);
2932

30-
void deviceManagerCheckPciDevices();
33+
static g_user_mutex devicesLock = g_mutex_initialize_r(true);
34+
static std::unordered_map<g_device_id, device_t*> devices;
35+
static g_device_id nextDeviceId = 1;
3136

3237
int main()
3338
{
34-
deviceManagerCheckPciDevices();
39+
g_tid comHandler = g_create_task((void*) _deviceManagerAwaitCommands);
40+
_deviceManagerCheckPciDevices();
41+
42+
g_join(comHandler);
3543
}
3644

37-
void deviceManagerCheckPciDevices()
45+
void _deviceManagerCheckPciDevices()
3846
{
3947
int num;
4048
g_pci_device_data* devices;
@@ -85,3 +93,57 @@ void deviceManagerCheckPciDevices()
8593
g_spawn("/applications/vbedriver.bin", "", "", G_SECURITY_LEVEL_DRIVER);
8694
}
8795
}
96+
97+
void _deviceManagerAwaitCommands()
98+
{
99+
if(!g_task_register_name(G_DEVICE_MANAGER_NAME))
100+
{
101+
klog("failed to register as %s", G_DEVICE_MANAGER_NAME);
102+
g_exit(-1);
103+
}
104+
105+
size_t bufLen = 1024;
106+
uint8_t buf[bufLen];
107+
108+
while(true)
109+
{
110+
auto status = g_receive_message(buf, bufLen);
111+
if(status != G_MESSAGE_RECEIVE_STATUS_SUCCESSFUL)
112+
continue;
113+
auto message = (g_message_header*) buf;
114+
auto content = (g_device_manager_header*) G_MESSAGE_CONTENT(message);
115+
116+
if(content->command == G_DEVICE_MANAGER_REGISTER_DEVICE)
117+
{
118+
_deviceManagerHandleRegisterDevice(message->sender, message->transaction,
119+
(g_device_manager_register_device_request*) content);
120+
}
121+
}
122+
}
123+
124+
void _deviceManagerHandleRegisterDevice(g_tid sender, g_message_transaction tx,
125+
g_device_manager_register_device_request* content)
126+
{
127+
g_mutex_acquire(devicesLock);
128+
auto id = nextDeviceId++;
129+
auto device = new device_t();
130+
device->id = id;
131+
device->handler = content->handler;
132+
device->type = content->type;
133+
devices[id] = device;
134+
g_mutex_release(devicesLock);
135+
136+
// Respond to registerer
137+
g_device_manager_register_device_response response{};
138+
response.status = G_DEVICE_MANAGER_SUCCESS;
139+
response.id = id;
140+
g_send_message_t(sender, &response, sizeof(response), tx);
141+
142+
// Post to topic
143+
g_device_event_device_registered event{};
144+
event.header.event = G_DEVICE_EVENT_DEVICE_REGISTERED;
145+
event.id = device->id;
146+
event.type = content->type;
147+
event.driver = device->handler;
148+
g_send_topic_message(G_DEVICE_EVENT_TOPIC, &event, sizeof(event));
149+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2+
* *
3+
* Ghost, a micro-kernel based operating system for the x86 architecture *
4+
* Copyright (C) 2025, Max Schlüssel <[email protected]> *
5+
* *
6+
* This program is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* *
11+
* This program is distributed in the hope that it will be useful, *
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14+
* GNU General Public License for more details. *
15+
* *
16+
* You should have received a copy of the GNU General Public License *
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
18+
* *
19+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
20+
21+
#ifndef DEVICE_MANAGER
22+
#define DEVICE_MANAGER
23+
24+
#include <libdevice/interface.hpp>
25+
26+
struct device_t
27+
{
28+
g_device_id id;
29+
g_device_type type;
30+
g_tid handler;
31+
};
32+
33+
#endif

applications/libahci/inc/libahci/driver.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#ifndef __LIBAHCI_AHCIDRIVER__
2222
#define __LIBAHCI_AHCIDRIVER__
2323

24-
#define G_AHCI_DRIVER_IDENTIFIER "ahcidriver"
24+
#define G_AHCI_DRIVER_NAME "ahcidriver"
2525

2626

2727
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2+
* *
3+
* Ghost, a micro-kernel based operating system for the x86 architecture *
4+
* Copyright (C) 2025, Max Schlüssel <[email protected]> *
5+
* *
6+
* This program is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* *
11+
* This program is distributed in the hope that it will be useful, *
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14+
* GNU General Public License for more details. *
15+
* *
16+
* You should have received a copy of the GNU General Public License *
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
18+
* *
19+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
20+
21+
#ifndef __LIBDEVICE_INTERFACE__
22+
#define __LIBDEVICE_INTERFACE__
23+
24+
#include <cstdint>
25+
#include <ghost/tasks/types.h>
26+
27+
#define G_DEVICE_MANAGER_NAME "devicemanager"
28+
29+
/**
30+
* Different device types the system can handle
31+
*/
32+
typedef uint16_t g_device_type;
33+
#define G_DEVICE_TYPE_VIDEO ((g_device_type) 1)
34+
#define G_DEVICE_TYPE_BLOCK ((g_device_type) 2)
35+
36+
/**
37+
* System wide device identifier given by the device manager on registration
38+
*/
39+
typedef uint16_t g_device_id;
40+
41+
/**
42+
* Commands that the device manager understands
43+
*/
44+
typedef uint16_t g_device_manager_command;
45+
#define G_DEVICE_MANAGER_REGISTER_DEVICE ((g_device_manager_command) 0)
46+
47+
/**
48+
* Generic response codes
49+
*/
50+
typedef uint8_t g_device_manager_general_status;
51+
#define G_DEVICE_MANAGER_SUCCESS ((g_device_manager_general_status) 1)
52+
#define G_DEVICE_MANAGER_ERROR ((g_device_manager_general_status) 2)
53+
54+
/**
55+
* Header of each request
56+
*/
57+
struct g_device_manager_header
58+
{
59+
g_device_manager_command command;
60+
}__attribute__((packed));
61+
62+
struct g_device_manager_register_device_request
63+
{
64+
g_device_manager_header header;
65+
g_device_type type;
66+
g_tid handler;
67+
}__attribute__((packed));
68+
69+
struct g_device_manager_register_device_response
70+
{
71+
g_device_manager_general_status status;
72+
g_device_id id;
73+
}__attribute__((packed));
74+
75+
/**
76+
* Messages that the manager posts to the device topic
77+
*/
78+
#define G_DEVICE_EVENT_TOPIC "device"
79+
80+
typedef uint16_t g_device_event;
81+
#define G_DEVICE_EVENT_DEVICE_REGISTERED ((g_device_event) 0)
82+
83+
struct g_device_event_header
84+
{
85+
g_device_event event;
86+
}__attribute__((packed));
87+
88+
struct g_device_event_device_registered
89+
{
90+
g_device_event_header header;
91+
g_device_id id;
92+
g_device_type type;
93+
g_tid driver;
94+
}__attribute__((packed));
95+
96+
97+
#endif

applications/libdevice/inc/libdevice/manager.hpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
#ifndef __LIBDEVICE_MANAGER__
2222
#define __LIBDEVICE_MANAGER__
2323

24-
// Interface to the device manager
24+
#include "interface.hpp"
2525

26-
struct g_device_manager_header {
27-
}__attribute__((packed));
26+
/**
27+
* Tells the device manager that a new device should be added and which task
28+
* is responsible for handling interaction with the device.
29+
*/
30+
bool deviceManagerRegisterDevice(g_device_type type, g_tid handler, g_device_id* outId);
2831

29-
#endif
32+
#endif

applications/libdevice/src/manager.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,38 @@
1919
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020

2121
#include "libdevice/manager.hpp"
22+
#include <ghost/messages.h>
23+
#include <ghost/tasks.h>
24+
#include <cstdio>
25+
26+
bool deviceManagerRegisterDevice(g_device_type type, g_tid handler, g_device_id* outId)
27+
{
28+
g_tid managerId = g_task_await_by_name(G_DEVICE_MANAGER_NAME);
29+
auto tx = g_get_message_tx_id();
30+
31+
g_device_manager_register_device_request request{};
32+
request.type = G_DEVICE_MANAGER_REGISTER_DEVICE;
33+
request.handler = handler;
34+
request.type = type;
35+
g_send_message_t(managerId, &request, sizeof(request), tx);
36+
37+
bool success = false;
38+
size_t bufLen = sizeof(g_message_header) + sizeof(g_device_manager_register_device_response);
39+
uint8_t buf[bufLen];
40+
if(g_receive_message_t(buf, bufLen, tx) == G_MESSAGE_RECEIVE_STATUS_SUCCESSFUL)
41+
{
42+
auto content = (g_device_manager_register_device_response*) G_MESSAGE_CONTENT(buf);
43+
44+
if(content->status == G_DEVICE_MANAGER_SUCCESS)
45+
{
46+
*outId = content->id;
47+
success = true;
48+
}
49+
else
50+
{
51+
klog("failed to register device with status %i", content->status);
52+
}
53+
}
54+
55+
return success;
56+
}

applications/libpci/inc/libpci/driver.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "pci.hpp"
2525
#include <ghost.h>
2626

27-
#define G_PCI_DRIVER_IDENTIFIER "pcidriver"
27+
#define G_PCI_DRIVER_NAME "pcidriver"
2828

2929
/**
3030
* Packed type to more easily pass device id

applications/libpci/src/driver.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
bool pciDriverListDevices(int* outCount, g_pci_device_data** outData)
2727
{
28-
g_tid driverTid = g_task_await_by_id(G_PCI_DRIVER_IDENTIFIER);
28+
g_tid driverTid = g_task_await_by_name(G_PCI_DRIVER_NAME);
2929

3030
g_message_transaction tx = g_get_message_tx_id();
3131

@@ -59,7 +59,7 @@ void pciDriverFreeDeviceList(g_pci_device_data* deviceList)
5959

6060
bool pciDriverReadConfig(g_pci_device_address address, uint8_t offset, int bytes, uint32_t* outValue)
6161
{
62-
g_tid driverTid = g_task_await_by_id(G_PCI_DRIVER_IDENTIFIER);
62+
g_tid driverTid = g_task_await_by_name(G_PCI_DRIVER_NAME);
6363

6464
g_message_transaction tx = g_get_message_tx_id();
6565

@@ -85,7 +85,7 @@ bool pciDriverReadConfig(g_pci_device_address address, uint8_t offset, int bytes
8585

8686
bool pciDriverWriteConfig(g_pci_device_address address, uint8_t offset, int bytes, uint32_t value)
8787
{
88-
g_tid driverTid = g_task_await_by_id(G_PCI_DRIVER_IDENTIFIER);
88+
g_tid driverTid = g_task_await_by_name(G_PCI_DRIVER_NAME);
8989

9090
g_message_transaction tx = g_get_message_tx_id();
9191

@@ -111,7 +111,7 @@ bool pciDriverWriteConfig(g_pci_device_address address, uint8_t offset, int byte
111111

112112
bool pciDriverEnableResourceAccess(g_pci_device_address address, bool enabled)
113113
{
114-
g_tid driverTid = g_task_await_by_id(G_PCI_DRIVER_IDENTIFIER);
114+
g_tid driverTid = g_task_await_by_name(G_PCI_DRIVER_NAME);
115115

116116
g_message_transaction tx = g_get_message_tx_id();
117117

@@ -134,7 +134,7 @@ bool pciDriverEnableResourceAccess(g_pci_device_address address, bool enabled)
134134

135135
bool pciDriverReadBAR(g_pci_device_address address, uint8_t bar, uint32_t* outValue)
136136
{
137-
g_tid driverTid = g_task_await_by_id(G_PCI_DRIVER_IDENTIFIER);
137+
g_tid driverTid = g_task_await_by_name(G_PCI_DRIVER_NAME);
138138

139139
g_message_transaction tx = g_get_message_tx_id();
140140

@@ -158,7 +158,7 @@ bool pciDriverReadBAR(g_pci_device_address address, uint8_t bar, uint32_t* outVa
158158

159159
bool pciDriverReadBARSize(g_pci_device_address address, uint8_t bar, uint32_t* outValue)
160160
{
161-
g_tid driverTid = g_task_await_by_id(G_PCI_DRIVER_IDENTIFIER);
161+
g_tid driverTid = g_task_await_by_name(G_PCI_DRIVER_NAME);
162162

163163
g_message_transaction tx = g_get_message_tx_id();
164164

applications/libps2/src/ps2.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ ps2_status_t ps2Initialize(void (*mouseCallback)(int16_t, int16_t, uint8_t, int8
6262

6363
void ps2AwaitKeyIrq()
6464
{
65-
g_task_register_id("libps2/await-key");
65+
g_task_register_name("libps2/await-key");
6666
for(;;)
6767
{
6868
g_await_irq_t(1, 50);
@@ -72,7 +72,7 @@ void ps2AwaitKeyIrq()
7272

7373
void ps2AwaitMouseIrq()
7474
{
75-
g_task_register_id("libps2/await-mouse");
75+
g_task_register_name("libps2/await-mouse");
7676
for(;;)
7777
{
7878
g_await_irq_t(12, 50);

0 commit comments

Comments
 (0)