Skip to content

Commit

Permalink
Merge pull request #423 from zhangxp1998/dev
Browse files Browse the repository at this point in the history
Implement bulk of UEFI protocol needed by linux kernel
  • Loading branch information
travisg authored Nov 7, 2024
2 parents dbef9ff + 649d432 commit afa5679
Show file tree
Hide file tree
Showing 33 changed files with 1,817 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ tagit
TAGS
tags
toolchain
.cache
vmlinux
9 changes: 9 additions & 0 deletions external/lib/heap/dlmalloc/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ LOCAL_DIR := $(GET_LOCAL_DIR)

MODULE := $(LOCAL_DIR)

# MSPACE=1 enables mspace_malloc and other mspace_* routines.
# They allow users to use preallocated memory for heap allocations
# It's common for VM applications to preallocate backing memory for
# the guest, then free the entire backing memory at once after guest
# exits. This ensures no memory leak even if guest doesn't free its
# memory properly. Or hypervisor may wish that the guest memory
# are all contigous, etc.
MODULE_DEFINES=MSPACES=1

MODULE_SRCS += \
$(LOCAL_DIR)/dlmalloc.c

Expand Down
3 changes: 2 additions & 1 deletion kernel/include/kernel/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ __BEGIN_CDECLS

#define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE)
#define IS_PAGE_ALIGNED(x) IS_ALIGNED(x, PAGE_SIZE)
struct list_node *get_arena_list(void);

struct mmu_initial_mapping {
paddr_t phys;
Expand Down Expand Up @@ -259,7 +260,7 @@ void vmm_context_switch(vmm_aspace_t *oldspace, vmm_aspace_t *newaspace);

/* set the current user aspace as active on the current thread.
NULL is a valid argument, which unmaps the current user address space */
void vmm_set_active_aspace(vmm_aspace_t *aspace);
vmm_aspace_t* vmm_set_active_aspace(vmm_aspace_t *aspace);

__END_CDECLS

Expand Down
2 changes: 2 additions & 0 deletions kernel/vm/pmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static mutex_t lock = MUTEX_INITIAL_VALUE(lock);
#define ADDRESS_IN_ARENA(address, arena) \
((address) >= (arena)->base && (address) <= (arena)->base + (arena)->size - 1)

struct list_node *get_arena_list() { return &arena_list; }

static inline bool page_is_free(const vm_page_t *page) {
return !(page->flags & VM_PAGE_FLAG_NONFREE);
}
Expand Down
12 changes: 8 additions & 4 deletions kernel/vm/vmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <lk/err.h>
#include <lk/trace.h>
#include <string.h>
#include "kernel/thread.h"
#include "vm_priv.h"

#define LOCAL_TRACE 0
Expand Down Expand Up @@ -751,21 +752,24 @@ void vmm_context_switch(vmm_aspace_t *oldspace, vmm_aspace_t *newaspace) {
arch_mmu_context_switch(newaspace ? &newaspace->arch_aspace : NULL);
}

void vmm_set_active_aspace(vmm_aspace_t *aspace) {
vmm_aspace_t* vmm_set_active_aspace(vmm_aspace_t *aspace) {
LTRACEF("aspace %p\n", aspace);

thread_t *t = get_current_thread();
DEBUG_ASSERT(t);

if (aspace == t->aspace)
return;
return aspace;

/* grab the thread lock and switch to the new address space */
THREAD_LOCK(state);
vmm_aspace_t *old = t->aspace;
t->aspace = aspace;
vmm_context_switch(old, t->aspace);
if (old != aspace) {
t->aspace = aspace;
vmm_context_switch(old, t->aspace);
}
THREAD_UNLOCK(state);
return old;
}

static void dump_region(const vmm_region_t *r) {
Expand Down
4 changes: 3 additions & 1 deletion lib/uefi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
make qemu-virt-arm64-test
```

Note, this may fail if your system does not have `aarch64-elf-gcc` installed. To address, download from [here](https://newos.org/toolchains/aarch64-elf-14.2.0-Linux-x86_64.tar.xz), unzip, and add the extracted dir to PATH.

## Run

```
qemu-system-aarch64 -cpu max -m 512 -smp 1 -machine virt,highmem=off \
-kernel qemu-virt-arm64-test/lk.elf \
-kernel build-qemu-virt-arm64-test/lk.elf \
-net none -nographic \
-drive if=none,file=lib/uefi/helloworld_aa64.efi,id=blk,format=raw \
-device virtio-blk-device,drive=blk
Expand Down
Loading

0 comments on commit afa5679

Please sign in to comment.