Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit 3def5ca

Browse files
committed
add app dir and simple app
1 parent 31ef27d commit 3def5ca

8 files changed

Lines changed: 76 additions & 27 deletions

File tree

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
"[c]": {
1919
"editor.tabSize": 8,
2020
"editor.formatOnSave": true,
21-
},
21+
},
22+
"files.associations": {
23+
"memory.h": "c"
24+
},
2225
}

Makefile

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ USER_SOURCES = $(shell find $(SRC_USER) -name "*.c" \! -name "syscall.c")
4141
USER_OBJECTS = $(shell printf "%s\n" $(patsubst $(SRC_USER)/%.c, $(OUT_DIR)/usr/%.o, $(USER_SOURCES)) | sort -u)
4242
USER_ELFS = $(shell printf "%s\n" $(patsubst $(SRC_USER)/%.c, $(OUT_DIR)/usr/%.elf, $(USER_SOURCES)) | sort -u)
4343

44+
## Determine apps source directory: prefer repo-root 'apps' if present, otherwise 'src/apps'
45+
ifeq ($(wildcard apps),apps)
4446
SRC_APPS = apps
47+
else
48+
SRC_APPS = $(SRC_DIR)/apps
49+
endif
4550
APP_OUT_DIR = $(OUT_DIR)/apps
4651
APP_SOURCES = $(shell if [ -d "$(SRC_APPS)" ]; then find $(SRC_APPS) -name "*.c"; fi)
4752
APP_OBJECTS = $(shell printf "%s\n" $(patsubst $(SRC_APPS)/%.c, $(APP_OUT_DIR)/%.o, $(APP_SOURCES)) | sort -u)
@@ -121,28 +126,29 @@ $(APP_OUT_DIR)/%.o: $(SRC_APPS)/%.c
121126
@$(CC) $(CFLAGS) -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR)/targ-include -c $< -o $@
122127

123128

129+
# Build a small syscall shim for apps so newlib's syscalls/_sbrk/_exit etc
130+
# resolve to int 0x80 stubs implemented in src/user/syscall.c
131+
$(APP_OUT_DIR)/syscall.o: $(SRC_USER)/syscall.c
132+
@mkdir -p $(dir $@)
133+
@$(CC) $(CFLAGS) -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR)/targ-include -c $< -o $@
134+
135+
124136
user: lib $(ALL_USER_ELFS) $(APP_ELFS)
125137
@echo "Built user ELFs: $(ALL_USER_ELFS)"
126138

127139

128140

129-
$(APP_OUT_DIR)/%.elf: $(APP_OUT_DIR)/%.o
141+
142+
$(APP_OUT_DIR)/%.elf: $(APP_OUT_DIR)/%.o $(APP_OUT_DIR)/syscall.o
130143
@mkdir -p $(dir $@)
131144
@echo "Linking app ELF: $@"
132145
@if [ -f "$(BIN_LIB_DIR)/libc.a" ]; then \
133-
$(CC) -nostdlib -static $< $(USER_LDFLAGS) -o $@; \
146+
$(CC) -nostdlib -static $^ $(USER_LDFLAGS) -o $@; \
134147
else \
135-
$(CC) -nostdlib -static $< -o $@; \
148+
$(CC) -nostdlib -static $^ -o $@; \
136149
fi
137150

138-
$(OUT_DIR)/user/%.elf: $(OUT_DIR)/user/%.o
139-
@mkdir -p $(dir $@)
140-
@echo "Linking user ELF: $@"
141-
@if [ -f "$(BIN_LIB_DIR)/libc.a" ]; then \
142-
$(CC) -nostdlib -static $< $(USER_LDFLAGS) -o $@; \
143-
else \
144-
$(CC) -nostdlib -static $< -o $@; \
145-
fi
151+
146152

147153

148154
$(OUT_DIR)/usr/hello.elf: $(OUT_DIR)/usr/hello.o $(OUT_DIR)/usr/syscall.o
@@ -196,19 +202,30 @@ $(ESP_IMG): $(BOOTX64) $(KERNEL)
196202
$(EXT2_IMG): $(KERNEL)
197203
@rm -f $(EXT2_IMG)
198204
@echo "Creating FAT16 filesystem image..."
205+
@rm -rf bin/fs_tmp || true
199206
@mkdir -p bin/fs_tmp/kernel/fonts
200207
@mkdir -p bin/fs_tmp/usr
201208
@mkdir -p bin/fs_tmp/apps
202209
@mkdir -p bin/fs_tmp/lib
203210
@cp -f $(FONTS) bin/fs_tmp/kernel/fonts/ 2>/dev/null || true
204-
@cp -f bin/usr/*.elf bin/fs_tmp/usr/ 2>/dev/null || true
205-
@cp -f bin/apps/*.elf bin/fs_tmp/apps/ 2>/dev/null || true
211+
@mkdir -p bin/fs_tmp/usr
212+
@# remove stale user/app ELFs in bin to avoid leftover artifacts
213+
@rm -f bin/usr/*.elf 2>/dev/null || true
214+
@rm -f bin/apps/*.elf 2>/dev/null || true
215+
@# copy only the ELFs we built (USER_ELFS and APP_ELFS)
216+
@for f in $(USER_ELFS); do \
217+
if [ -f "$$f" ]; then cp -f "$$f" bin/fs_tmp/usr/; fi; \
218+
done || true
219+
@for f in $(APP_ELFS); do \
220+
if [ -f "$$f" ]; then cp -f "$$f" bin/fs_tmp/apps/; fi; \
221+
done || true
206222
@cp -f bin/lib/* bin/fs_tmp/lib/ 2>/dev/null || true
207223
@find bin -type f \
208224
-not -name "*.o" \
209225
-not -name "fs.img" \
210226
-not -path "bin/fs_tmp/*" \
211227
-not -path "bin/usr/*" \
228+
-not -path "bin/user/*" \
212229
-exec bash -c 'dest="bin/fs_tmp/$${1#bin/}"; mkdir -p "$$(dirname "$$dest")"; cp "$$1" "$$dest"' _ {} \;
213230
@mkdir -p bin/fs_tmp
214231
@cp README.md bin/fs_tmp/README.md 2>/dev/null || true

src/apps/hello.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
int main() {
6+
printf("Hello, world!\n");
7+
printf("This application is LiteCore's first app!\n");
8+
9+
size_t len = 64;
10+
char *buf = malloc(len);
11+
if (!buf) {
12+
printf("malloc failed\n");
13+
return 1;
14+
}
15+
snprintf(buf, len, "Allocated %zu bytes at %p", len, (void *)buf);
16+
printf("%s\n", buf);
17+
18+
size_t newlen = 128;
19+
char *tmp = realloc(buf, newlen);
20+
if (!tmp) {
21+
printf("realloc failed, freeing original buffer\n");
22+
free(buf);
23+
return 1;
24+
}
25+
buf = tmp;
26+
size_t used = strlen(buf);
27+
snprintf(buf + used, newlen - used, " (resized)");
28+
printf("%s\n", buf);
29+
30+
free(buf);
31+
buf = NULL;
32+
printf("Memory freed\n");
33+
34+
printf("Goodbye! ;)\n");
35+
36+
return 0;
37+
}

src/kernel/main.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ void kmain(BOOT_INFO *boot_info) {
5656
new_line();
5757
printk("====== TESTS ======\n");
5858
run_test();
59-
60-
// マルチタスクテストを実行
61-
new_line();
62-
multi_task_test();
6359
#endif /* TEST_TRUE */
6460

6561
new_line();

src/kernel/shell/extended_commands.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,16 +627,14 @@ static int cmd_run(int argc, char **argv) {
627627
uint8_t *dst = (uint8_t *)(uintptr_t)frame_virt;
628628
/* calculate portion of file that maps into this page */
629629
uint32_t page_va = dest_virt;
630-
uint64_t copy_from;
631630
if (page_va + 0x1000 <= vaddr) {
632631
/* page before file data start */
633-
copy_from = 0;
632+
uint64_t copy_from = 0;
634633
}
635634
/* For simplicity, copy relevant bytes: */
636635
uint64_t seg_file_end = off + filesz;
637-
uint64_t src_off;
638636
if ((uint64_t)page_va + 0x1000 <= vaddr) {
639-
src_off = 0; /* nothing from file */
637+
uint64_t src_off = 0; /* nothing from file */
640638
} else {
641639
/* overlapping region */
642640
uint64_t file_page_offset = 0;

src/kernel/task/multi_task.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ static int scheduler_enabled = 0;
2727
// idle タスク(常に実行可能)
2828
static task_t idle_task;
2929

30-
/**
31-
* @brief idleタスクのエントリポイント
32-
*/
30+
/*
3331
static void idle_task_entry(void) {
3432
while (1) {
3533
asm volatile("hlt");
3634
}
3735
}
36+
*/
3837

3938
/**
4039
* @brief 文字列コピー(簡易実装)

src/kernel/util/console.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ void new_line() {
305305
* @brief 位置文字表示
306306
*/
307307
static void console_putc(char ch) {
308-
uint8_t attr = COLOR;
309308

310309
if (ch == '\n') {
311310
new_line();

tools/fmt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
find src/kernel src/user boot -type f \( -name "*.c" -o -name "*.h" \) -exec clang-format -i {} +
1+
find src/kernel src/user src/apps boot -type f \( -name "*.c" -o -name "*.h" \) -exec clang-format -i {} +

0 commit comments

Comments
 (0)