Skip to content

Commit 5337363

Browse files
committed
tmp push
1 parent 4b9ccc5 commit 5337363

File tree

140 files changed

+9458
-721
lines changed

Some content is hidden

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

140 files changed

+9458
-721
lines changed

.clang-tidy

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bugprone-multiple-statement-macro,
2424
bugprone-no-escape,
2525
bugprone-parent-virtual-call,
2626
bugprone-posix-return,
27-
bugprone-reserved-identifier,
27+
-bugprone-reserved-identifier,
2828
bugprone-sizeof-container,
2929
bugprone-sizeof-expression,
3030
bugprone-spuriously-wake-up-functions,
@@ -143,4 +143,9 @@ readability-static-accessed-through-instance,
143143
readability-static-definition-in-anonymous-namespace,
144144
readability-string-compare,
145145
readability-uniqueptr-delete-release,
146-
readability-use-anyofallof'
146+
readability-use-anyofallof'
147+
#CheckOptions:
148+
# - key: bugprone-assert-side-effect.AssertMacros
149+
# value: 'assert'
150+
# - key: bugprone-assert-side-effect.CheckFunctionCalls
151+
# value: 'true'

CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.27)
2-
project(crescent CXX ASM)
2+
project(crescent C CXX ASM)
33

44
set(CMAKE_CXX_STANDARD 23)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -61,8 +61,8 @@ if(ARCH STREQUAL "x86_64")
6161

6262
target_link_libraries(crescent PRIVATE qacpi::qacpi qacpi::events)
6363
elseif(ARCH STREQUAL "aarch64")
64-
target_compile_options(crescent PRIVATE -fno-PIE -mno-unaligned-access)
65-
target_link_options(crescent PRIVATE -T ${PROJECT_SOURCE_DIR}/lds/aarch64.ld)
64+
target_compile_options(crescent PRIVATE -fPIE)
65+
target_link_options(crescent PRIVATE -T ${PROJECT_SOURCE_DIR}/lds/aarch64.ld -static-pie)
6666
target_compile_definitions(crescent PRIVATE ARCH_AARCH64)
6767

6868
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
@@ -87,13 +87,17 @@ target_sources(crescent PUBLIC FILE_SET HEADERS
8787
FILES
8888

8989
include/crescent/aarch64/arch_syscall.h
90+
include/crescent/aarch64/posix_arch_syscall.h
9091
include/crescent/x86/arch_syscall.h
92+
include/crescent/x86/posix_arch_syscall.h
9193
include/crescent/devlink.h
9294
include/crescent/event.h
9395
include/crescent/evm.h
9496
include/crescent/socket.h
9597
include/crescent/syscall.h
9698
include/crescent/syscalls.h
99+
include/crescent/posix_syscall.h
100+
include/crescent/posix_syscalls.h
97101
include/crescent/time.h
98102
)
99103

CMakeOptions.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,17 @@ if(CONFIG_PCI AND CONFIG_USB_XHCI)
2020
option(CONFIG_XHCI_PCI "Enable xhci pci support" ON)
2121
endif()
2222

23+
if(CONFIG_DTB AND CONFIG_USB_XHCI)
24+
option(CONFIG_XHCI_DTB "Enable xhci dtb support" ON)
25+
endif()
26+
27+
option(CONFIG_UFS "Enable ufs support" ON)
28+
if(CONFIG_PCI AND CONFIG_UFS)
29+
option(CONFIG_UFS_PCI "Enable ufs pci support" ON)
30+
endif()
31+
if(CONFIG_DTB AND CONFIG_UFS)
32+
option(CONFIG_UFS_DTB "Enable ufs dtb support" ON)
33+
endif()
34+
2335
configure_file(config.hpp.in config.hpp @ONLY)
2436
target_include_directories(crescent PRIVATE "${CMAKE_BINARY_DIR}")

apps/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ endmacro()
1515

1616
add_subdirectory(desktop)
1717
add_subdirectory(console)
18+
add_subdirectory(init)
1819

1920
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
2021
add_subdirectory(evm)

apps/console/src/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ namespace protocol = windower::protocol;
1111
using namespace std::literals;
1212

1313
int main() {
14+
CrescentHandle bash_stdout_read_handle;
15+
CrescentHandle bash_stdout_write_handle;
16+
sys_pipe_create(bash_stdout_read_handle, bash_stdout_write_handle, 0x1000, OPEN_NONBLOCK, 0);
17+
18+
CrescentHandle bash_handle;
19+
ProcessCreateInfo bash_info {
20+
.args = nullptr,
21+
.arg_count = 0,
22+
.stdin_handle = STDIN_HANDLE,
23+
.stdout_handle = bash_stdout_write_handle,
24+
.stderr_handle = STDERR_HANDLE,
25+
.flags = PROCESS_STD_HANDLES
26+
};
27+
sys_process_create(bash_handle, "/usr/bin/bash", sizeof("/usr/bin/bash") - 1, bash_info);
28+
1429
uint32_t colors[] {
1530
0xFF0000,
1631
0x00FF00,
@@ -43,6 +58,13 @@ int main() {
4358
window.redraw();
4459

4560
while (true) {
61+
char buf[512] {};
62+
size_t bash_read = 0;
63+
sys_read(bash_stdout_read_handle, buf, sizeof(buf), &bash_read);
64+
if (bash_read) {
65+
printf("got bash stdout '%s'\n", buf);
66+
}
67+
4668
auto event = window.wait_for_event();
4769
if (event.type == protocol::WindowEvent::CloseRequested) {
4870
window.close();

apps/desktop/src/main.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ui/context.hpp>
77
#include <ui/text.hpp>
88
#include <cassert>
9+
#include <mutex>
910
#include <stdio.h>
1011
#include <string.h>
1112

@@ -62,8 +63,8 @@ struct Connection {
6263
CrescentHandle event;
6364
};
6465

65-
// todo mutex
66-
static NoDestroy<std::vector<std::unique_ptr<Connection>>> CONNECTIONS {};
66+
static std::vector<std::unique_ptr<Connection>> CONNECTIONS {};
67+
static std::mutex CONNECTIONS_MUTEX;
6768

6869
struct WindowInfo {
6970
ui::Window* window;
@@ -141,9 +142,11 @@ void listener_thread(void* arg) {
141142
resp.type = protocol::Response::Connected;
142143
resp.connected.event_handle = event_read_handle;
143144
// todo check status
144-
while (sys_socket_send(connection_socket, &resp, sizeof(resp)) == ERR_TRY_AGAIN);
145+
size_t actual;
146+
while (sys_socket_send(connection_socket, &resp, sizeof(resp), actual) == ERR_TRY_AGAIN);
145147

146-
CONNECTIONS->push_back(std::make_unique<Connection>(Connection {
148+
std::unique_lock guard {CONNECTIONS_MUTEX};
149+
CONNECTIONS.push_back(std::make_unique<Connection>(Connection {
147150
.process = peer_addr.target,
148151
.control = connection_socket,
149152
.event = event_write_handle
@@ -184,6 +187,20 @@ static void destroy_window(Desktop& desktop, ui::Window* window) {
184187
}
185188
}
186189

190+
struct Timer {
191+
void start() {
192+
sys_get_time(&_start);
193+
}
194+
195+
[[nodiscard]] uint64_t end() const {
196+
uint64_t now;
197+
sys_get_time(&now);
198+
return now - _start;
199+
}
200+
201+
uint64_t _start {};
202+
};
203+
187204
int main() {
188205
while (false) {
189206
DevLinkRequest request {
@@ -624,13 +641,15 @@ int main() {
624641
uint64_t last_time_update_ns;
625642
status = sys_get_time(&last_time_update_ns);
626643
assert(status == 0);
644+
//uint64_t last_fps = last_time_update_ns;
627645

628646
while (true) {
629647
uint64_t start_time_ns;
630648
sys_get_time(&start_time_ns);
631649

632-
for (size_t i = 0; i < CONNECTIONS->size();) {
633-
auto& connection = (*CONNECTIONS)[i];
650+
CONNECTIONS_MUTEX.lock();
651+
for (size_t i = 0; i < CONNECTIONS.size();) {
652+
auto& connection = CONNECTIONS[i];
634653

635654
protocol::Request req {};
636655
size_t received;
@@ -657,7 +676,7 @@ int main() {
657676
}
658677
}
659678

660-
CONNECTIONS->erase(CONNECTIONS->begin() + (&connection - CONNECTIONS->data()));
679+
CONNECTIONS.erase(CONNECTIONS.begin() + (&connection - CONNECTIONS.data()));
661680
continue;
662681
}
663682

@@ -704,7 +723,8 @@ int main() {
704723
});
705724

706725
// todo check status
707-
while (sys_socket_send(connection->control, &resp, sizeof(resp)) == ERR_TRY_AGAIN);
726+
size_t actual;
727+
while (sys_socket_send(connection->control, &resp, sizeof(resp), actual) == ERR_TRY_AGAIN);
708728

709729
break;
710730
}
@@ -714,7 +734,8 @@ int main() {
714734
destroy_window(desktop, window);
715735

716736
// todo check status
717-
while (sys_socket_send(connection->control, &resp, sizeof(resp)) == ERR_TRY_AGAIN);
737+
size_t actual;
738+
while (sys_socket_send(connection->control, &resp, sizeof(resp), actual) == ERR_TRY_AGAIN);
718739

719740
break;
720741
}
@@ -725,14 +746,16 @@ int main() {
725746

726747
resp.ack.window_handle = window;
727748
// todo check status
728-
while (sys_socket_send(connection->control, &resp, sizeof(resp)) == ERR_TRY_AGAIN);
749+
size_t actual;
750+
while (sys_socket_send(connection->control, &resp, sizeof(resp), actual) == ERR_TRY_AGAIN);
729751

730752
break;
731753
}
732754
}
733755

734756
++i;
735757
}
758+
CONNECTIONS_MUTEX.unlock();
736759

737760
while (true) {
738761
InputEvent event;
@@ -814,6 +837,12 @@ int main() {
814837
uint64_t end_time_ns;
815838
sys_get_time(&end_time_ns);
816839
auto elapsed = end_time_ns - start_time_ns;
840+
841+
/*if (end_time_ns - last_fps >= NS_IN_S) {
842+
printf("elapsed %lu ns (%f fps)\n", elapsed, static_cast<float>(NS_IN_S) / static_cast<float>(elapsed));
843+
last_fps = end_time_ns;
844+
}*/
845+
817846
if (elapsed < NS_IN_S / 144) {
818847
auto remaining = NS_IN_S / 144 - elapsed;
819848
sys_sleep(remaining);

apps/evm/src/main.cpp

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,128 @@
66

77
namespace protocol = windower::protocol;
88

9+
#include "../../../include/crescent/evm.h"
910
#include "vga.hpp"
1011
#include "vm.hpp"
1112

13+
asm(R"(
14+
.globl some_code
15+
.globl some_code_end
16+
some_code:
17+
hlt
18+
some_code_end:
19+
)");
20+
21+
extern char some_code[];
22+
extern char some_code_end[];
23+
24+
#include <cassert>
25+
#include <cstdio>
26+
27+
#define CR0_PE 1u
28+
#define CR0_MP (1U << 1)
29+
#define CR0_EM (1U << 2)
30+
#define CR0_TS (1U << 3)
31+
#define CR0_ET (1U << 4)
32+
#define CR0_NE (1U << 5)
33+
#define CR0_WP (1U << 16)
34+
#define CR0_AM (1U << 18)
35+
#define CR0_NW (1U << 29)
36+
#define CR0_CD (1U << 30)
37+
#define CR0_PG (1U << 31)
38+
39+
/* CR4 bits */
40+
#define CR4_VME 1
41+
#define CR4_PVI (1U << 1)
42+
#define CR4_TSD (1U << 2)
43+
#define CR4_DE (1U << 3)
44+
#define CR4_PSE (1U << 4)
45+
#define CR4_PAE (1U << 5)
46+
#define CR4_MCE (1U << 6)
47+
#define CR4_PGE (1U << 7)
48+
#define CR4_PCE (1U << 8)
49+
#define CR4_OSFXSR (1U << 8)
50+
#define CR4_OSXMMEXCPT (1U << 10)
51+
#define CR4_UMIP (1U << 11)
52+
#define CR4_VMXE (1U << 13)
53+
#define CR4_SMXE (1U << 14)
54+
#define CR4_FSGSBASE (1U << 16)
55+
#define CR4_PCIDE (1U << 17)
56+
#define CR4_OSXSAVE (1U << 18)
57+
#define CR4_SMEP (1U << 20)
58+
#define CR4_SMAP (1U << 21)
59+
60+
#define EFER_SCE 1
61+
#define EFER_LME (1U << 8)
62+
#define EFER_LMA (1U << 10)
63+
#define EFER_NXE (1U << 11)
64+
65+
/* 32-bit page directory entry bits */
66+
#define PDE32_PRESENT 1
67+
#define PDE32_RW (1U << 1)
68+
#define PDE32_USER (1U << 2)
69+
#define PDE32_PS (1U << 7)
70+
71+
/* 64-bit page * entry bits */
72+
#define PDE64_PRESENT 1
73+
#define PDE64_RW (1U << 1)
74+
#define PDE64_USER (1U << 2)
75+
#define PDE64_ACCESSED (1U << 5)
76+
#define PDE64_DIRTY (1U << 6)
77+
#define PDE64_PS (1U << 7)
78+
#define PDE64_G (1U << 8)
79+
1280
int main() {
13-
windower::Windower windower;
81+
CrescentHandle evm;
82+
assert(sys_evm_create(evm) == 0);
83+
CrescentHandle vcpu;
84+
EvmGuestState* regs;
85+
assert(sys_evm_create_vcpu(evm, vcpu, &regs) == 0);
86+
87+
void* mem = nullptr;
88+
assert(sys_map(&mem, 0x200000, CRESCENT_PROT_READ | CRESCENT_PROT_WRITE) == 0);
89+
assert(sys_evm_map(evm, 0, mem, 0x200000) == 0);
90+
91+
memcpy(mem, some_code, some_code_end - some_code);
92+
93+
uint64_t pml4_addr = 0x2000;
94+
uint64_t *pml4 = (uint64_t *)((uintptr_t)mem + pml4_addr);
95+
96+
uint64_t pdpt_addr = 0x3000;
97+
uint64_t *pdpt = (uint64_t *)((uintptr_t)mem + pdpt_addr);
98+
99+
uint64_t pd_addr = 0x4000;
100+
uint64_t *pd = (uint64_t *)((uintptr_t)mem + pd_addr);
101+
102+
pml4[0] = PDE64_PRESENT | PDE64_RW | PDE64_USER | pdpt_addr;
103+
pdpt[0] = PDE64_PRESENT | PDE64_RW | PDE64_USER | pd_addr;
104+
pd[0] = PDE64_PRESENT | PDE64_RW | PDE64_USER | PDE64_PS;
105+
106+
regs->cr3 = pml4_addr;
107+
regs->cr4 = CR4_PAE;
108+
regs->cr0
109+
= CR0_PE | CR0_MP | CR0_ET | CR0_NE | CR0_WP | CR0_AM | CR0_PG;
110+
regs->efer = EFER_LME | EFER_LMA;
111+
112+
regs->cs.base = 0;
113+
regs->cs.selector = 1 << 3;
114+
regs->cs.limit = 0xffffffff;
115+
116+
regs->ds.base = 0;
117+
regs->ds.selector = 2 << 3;
118+
regs->es = regs->ds;
119+
regs->ss = regs->ds;
120+
regs->fs = regs->ds;
121+
regs->gs = regs->ds;
122+
regs->rflags = 2;
123+
regs->rip = 0;
124+
regs->rsp = 2 << 20;
125+
126+
assert(sys_evm_vcpu_write_state(vcpu, EVM_STATE_BITS_SEG_REGS | EVM_STATE_BITS_CONTROL_REGS | EVM_STATE_BITS_EFER) == 0);
127+
int res = sys_evm_vcpu_run(vcpu);
128+
printf("res: %d\n", res);
129+
130+
/*windower::Windower windower;
14131
if (auto status = windower::Windower::connect(windower); status != 0) {
15132
puts("[console]: failed to connect to window manager");
16133
return 1;
@@ -50,5 +167,5 @@ int main() {
50167
51168
window.redraw();
52169
}
53-
}
170+
}*/
54171
}

apps/init/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
APP(init
2+
src/main.cpp
3+
)
4+
target_link_libraries(init PRIVATE common)

0 commit comments

Comments
 (0)