From 6d9c743714f5e451ee0d855f00e6993cd5dc3599 Mon Sep 17 00:00:00 2001 From: wangguokun Date: Tue, 3 Dec 2024 17:26:39 +0800 Subject: [PATCH] plat/raspi: refactor static page table Signed-off-by: wangguokun --- Linker.uk | 31 +++++++++--- Makefile.uk | 5 +- console.c | 2 + raspi_bpt64.S | 102 -------------------------------------- raspi_mrds.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++ start.S | 1 - 6 files changed, 162 insertions(+), 112 deletions(-) delete mode 100644 raspi_bpt64.S create mode 100644 raspi_mrds.c diff --git a/Linker.uk b/Linker.uk index 15a1dfd..098ec3b 100644 --- a/Linker.uk +++ b/Linker.uk @@ -8,20 +8,32 @@ endif ## Link image ## RASPI_IMAGE := $(BUILD_DIR)/$(CONFIG_UK_NAME)_raspi-$(CONFIG_UK_ARCH) -RASPI_ELF_IMAGE := $(RASPI_IMAGE).elf +RASPI_DEBUG_IMAGE := $(RASPI_IMAGE).dbg +RASPI_STATIC_PT := $(BUILD_DIR)/tn_static_pgtable +ifeq ($(CONFIG_STATIC_PGTABLE),y) +RASPI_STATIC_PT_OBLIB := $(RASPI_STATIC_PT).o +else +RASPI_STATIC_PT_OBLIB := +endif RASPI_LD_SCRIPT_FLAGS := $(addprefix -Wl$(comma)-dT$(comma),\ $(UK_PLAT_RASPI_DEF_LDS)) RASPI_LD_SCRIPT_FLAGS += $(addprefix -Wl$(comma)-T$(comma),\ $(LIBRASPIPLAT_DEF_LDS) $(EXTRA_LD_SCRIPT-y)) -$(RASPI_ELF_IMAGE): $(RASPI_ALIBS) $(RASPI_ALIBS-y) $(RASPI_OLIBS) $(RASPI_OLIBS-y) \ - $(UK_ALIBS) $(UK_ALIBS-y) $(UK_OLIBS) $(UK_OLIBS-y) +ifeq ($(CONFIG_STATIC_PGTABLE),y) +$(RASPI_STATIC_PT_OBLIB): $(RASPI_OLIBS-y) $(STATIC_PAGE_TOOLS) + $(call build_static_pgtable,$(word 1, $<),$(RASPI_STATIC_PT)) +endif + +$(RASPI_DEBUG_IMAGE): $(RASPI_ALIBS) $(RASPI_ALIBS-y) $(RASPI_OLIBS) $(RASPI_OLIBS-y) \ + $(UK_ALIBS) $(UK_ALIBS-y) $(UK_OLIBS) $(UK_OLIBS-y) $(RASPI_STATIC_PT_OBLIB) $(call build_cmd,LD,,$(RASPI_IMAGE).ld.o,\ $(LD) -r $(LIBLDFLAGS) $(LIBLDFLAGS-y) \ $(RASPI_LDFLAGS) $(RASPI_LDFLAGS-y) \ $(RASPI_OLIBS) $(RASPI_OLIBS-y) \ $(UK_OLIBS) $(UK_OLIBS-y) \ + $(RASPI_STATIC_PT_OBLIB) \ -lgcc \ -Wl$(comma)--start-group \ $(RASPI_ALIBS) $(RASPI_ALIBS-y) \ @@ -37,26 +49,29 @@ $(RASPI_ELF_IMAGE): $(RASPI_ALIBS) $(RASPI_ALIBS-y) $(RASPI_OLIBS) $(RASPI_OLIBS $(RASPI_LD_SCRIPT_FLAGS) \ $(RASPI_IMAGE).o -o $@) $(call build_bootinfo,$@) +ifeq ($(CONFIG_STATIC_PGTABLE),y) + $(call update_static_pgtable,$@,$(RASPI_STATIC_PT)) +endif -$(RASPI_IMAGE): $(RASPI_IMAGE).elf +$(RASPI_IMAGE): $(RASPI_IMAGE).dbg $(call build_cmd,SCSTRIP,,$@,\ $(STRIP) -s \ $(SECT_STRIP_FLAGS) $(SECT_STRIP_FLAGS-y) \ $< -o $@ 2>&1 | \ { grep -v "Empty loadable segment detected" || true; }) $(call build_cmd,OBJCOPY,,$(RASPI_IMAGE).img,\ - $(OBJCOPY) -O binary $(RASPI_ELF_IMAGE) $(BUILD_DIR)/kernel8.img) + $(OBJCOPY) -O binary $(RASPI_DEBUG_IMAGE) $(BUILD_DIR)/kernel8.img) # register images to the build ifeq ($(CONFIG_PLAT_RASPI),y) -UK_DEBUG_IMAGES-y += $(RASPI_ELF_IMAGE) +UK_DEBUG_IMAGES-y += $(RASPI_DEBUG_IMAGE) UK_IMAGES-y += $(RASPI_IMAGE) endif # ...for cleaning: LIBRASPIPLAT_CLEAN += $(call build_clean,$(RASPI_IMAGE).o) LIBRASPIPLAT_CLEAN += $(call build_clean,$(RASPI_IMAGE).ld.o) -LIBRASPIPLAT_CLEAN += $(call build_clean,$(RASPI_IMAGE).elf) +LIBRASPIPLAT_CLEAN += $(call build_clean,$(RASPI_IMAGE).dbg) LIBRASPIPLAT_CLEAN += $(call build_clean,$(RASPI_IMAGE).img) -LIBRASPIPLAT_CLEAN += $(call build_clean,$(RASPI_ELF_IMAGE).bootinfo) +LIBRASPIPLAT_CLEAN += $(call build_clean,$(RASPI_DEBUG_IMAGE).bootinfo) LIBRASPIPLAT_CLEAN += $(call build_clean,$(BUILD_DIR)/kernel8.img) diff --git a/Makefile.uk b/Makefile.uk index 1aaa306..3d35c89 100644 --- a/Makefile.uk +++ b/Makefile.uk @@ -72,8 +72,11 @@ LIBRASPIPLAT_SRCS-y += $(LIBRASPIPLAT_BASE)/memory.c LIBRASPIPLAT_SRCS-y += $(LIBRASPIPLAT_BASE)/setup.c LIBRASPIPLAT_SRCS-y += $(LIBRASPIPLAT_BASE)/shutdown.c LIBRASPIPLAT_SRCS-$(CONFIG_ARCH_ARM_64) += $(UK_PLAT_COMMON_BASE)/arm/exceptions64.S|isr +ifeq ($(CONFIG_STATIC_PGTABLE),y) +LIBRASPIPLAT_SRCS-$(CONFIG_STATIC_PGTABLE) += $(LIBKVMPLAT_BASE)/arm/static_memregion.lds.S|arm +LIBRASPIPLAT_SRCS-$(CONFIG_STATIC_PGTABLE) += $(LIBRASPIPLAT_BASE)/raspi_mrds.c +endif LIBRASPIPLAT_SRCS-$(CONFIG_ARCH_ARM_64) += $(LIBRASPIPLAT_BASE)/mmu.S|isr -LIBRASPIPLAT_SRCS-$(CONFIG_ARCH_ARM_64) += $(LIBRASPIPLAT_BASE)/raspi_bpt64.S|arm LIBRASPIPLAT_SRCS-y += $(UK_PLAT_RASPI_DEF_LDS) LIBRASPIPLAT_SRCS-$(CONFIG_ARCH_ARM_64) += $(UK_PLAT_COMMON_BASE)/arm/traps_arm64.c|isr diff --git a/console.c b/console.c index 5bebb3c..dde10ce 100644 --- a/console.c +++ b/console.c @@ -26,7 +26,9 @@ void _libraspiplat_init_console(void *dtb) { +#if defined(CONFIG_LIBUKTTY_PL011_EARLY_CONSOLE_BASE) PL011_REG_WRITE(REG_UARTCR_OFFSET, 0); +#endif /* set up clock for consistent divisor values */ /* ARM to VedioCore*/ mbox_msg_create(9, 4*9, MBOX_REQUEST, MBOX_TAG_SETCLKRATE, diff --git a/raspi_bpt64.S b/raspi_bpt64.S deleted file mode 100644 index fb95e9b..0000000 --- a/raspi_bpt64.S +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2024 Hangzhou Yingyi Technology Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include -/* ------------------------- Memory Map of raspi ----------------------- - * - * 0x0000000000000 - 0x000003EFFFFFF NormalMem: 0-1008M attr:normal - * 0x000003F000000 - 0x000003FFFFFFF Device: 1008MiB-1024MiB attr:device - * 0x0000040000000 - 0x00000401FFFFF Peripheral:1024MiB-1026MiB attr:device - * - * Notice: The page tables below use the Unikraft indexing convention. - */ - -.section .data -.align 4 -.global bpt_unmap_mrd -bpt_unmap_mrd: - .quad 0x000000000001000 /* 4K */ - .quad 0x000000000001000 /* 4k */ - /* Used for struct ukplat_memregion_desc - * Unmapping starts at 4K and ends at 1G - */ - .quad 0x000000003EFFF000 - .short 0x0000000000000000 - .short 0x0000000000000010 /* UKPLAT_MEMRF_UNMAP */ - .space 36 - -.global arm64_bpt_l3_pt0 -/* L3: 0 - 256TiB (512GiB / entry) - * - * 0x0000000000000 - 0x0007fffffffff Table descriptor to l2_pt0 - * 0x0008000000000 - 0x0FF7FFFFFFFFF Unmapped - * 0x0ff8000000000 - 0x0ffffffffffff Table descriptor to l2_pt511 - */ -.align 12 -arm64_bpt_l3_pt0: - ur_pte arm64_bpt_l2_pt0, PTE_TYPE_TABLE - pte_zero , 510 -#if CONFIG_PAGING - ur_pte arm64_bpt_l2_pt511, PTE_TYPE_TABLE -#else /* !CONFIG_PAGING */ - pte_zero , 1 -#endif /* !CONFIG_PAGING */ - -/* L2: 0 - 512GiB (1GiB / entry) - * - * 0x0000000000000000 - 0x000000003FFFFFFF Table descriptor to l1_pt0 - * 0x0000000040000000 - 0x000000007FFFFFFF Table descriptor to l1_pt1 - * 0x0000000080000000 - 0x0000007FFFFFFFFF Unmapped - */ -.align 12 -arm64_bpt_l2_pt0: - ur_pte arm64_bpt_l1_pt0, PTE_TYPE_TABLE - ur_pte arm64_bpt_l1_pt1, PTE_TYPE_TABLE - pte_zero ,510 - -#if CONFIG_PAGING -/* L2: 255.5 TiB - 256TiB (1GiB / entry) - * - * 0x0000ff8000000000 - 0x0000ffffffffffff Direct-mapped - */ -.align 12 -arm64_bpt_l2_pt511: - pte_fill 0x0000000000000000, 1, 2, PTE_BLOCK_NORMAL_RW - pte_zero , 511 -#endif /* CONFIG_PAGING */ - -/* L1: 0 - 1GiB (2MiB / entry) - * 0x0000000000 - 0x00003EFFFFFF NormalMem 948M - 1008MiB attr:normal - * 0x003F000000 - 0x00003FFFFFFF Device: 1008MiB - 1024MiB attr: device - * There could be an arm64_bpt_l0_pt0,but it has no attribute difference - * from arm64_bpt_l1_pt0's first page,and 512 entries of 2MiB will cover all - * raspi memory,so it is not necessary for an arm64_bpt_l0_pt0. - */ -.align 12 -arm64_bpt_l1_pt0: - pte_fill 0x0000000000000000, 504, 1, PTE_BLOCK_NORMAL_RWX - pte_fill 0x000000003F000000, 8, 1, PTE_BLOCK_DEVICE_nGnRnE - -/* L1: 1 - 2GiB (2MiB / entry)(0-2Mi covered by arm64_bpt_l0_pt0 ) - * 0x0000000040000000 - 0x00007fffffff device @1GiB device - */ -.align 12 -arm64_bpt_l1_pt1: - pte_fill 0x0000000040000000, 1, 1, PTE_BLOCK_DEVICE_nGnRnE - pte_zero , 511 diff --git a/raspi_mrds.c b/raspi_mrds.c new file mode 100644 index 0000000..5e56c2e --- /dev/null +++ b/raspi_mrds.c @@ -0,0 +1,133 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2024 The TenonOS Authors + */ + +#include +#include + +/* ------------------------- Memory Map of raspi ----------------------- + * + * 0x0000000000000 - 0x000003EFFFFFF NormalMem: 0-1008M attr:normal + * 0x000003F000000 - 0x000003FFFFFFF Device: 1008MiB-1024MiB attr:device + * 0x0000040000000 - 0x00000401FFFFF Peripheral:1024MiB-1026MiB attr:device + * + * Notice: The page tables below use the Unikraft indexing convention. + */ +struct ukplat_memregion_desc bpt_unmap_mrd __section(".data") = { + .pbase = ALIGN_DOWN(0x1000UL, __PAGE_SIZE), + .vbase = ALIGN_DOWN(0x1000UL, __PAGE_SIZE), + .pg_off = 0x0UL, + .len = 0x3EFFF000UL, + .pg_count = PAGE_COUNT(0x3EFFF000UL), + .type = UKPLAT_MEMRT_RESERVED, + .flags = UKPLAT_MEMRF_UNMAP +#ifdef CONFIG_UKPLAT_MEMRNAME + , + .name = { 0 } +#endif +}; + +#ifdef CONFIG_STATIC_PGTABLE +struct ukplat_memregion_desc + bpt_memregion[] __section(".tn_static_memregion") = { + /* RAM + * Virtual address: 0x0000000000000000 - 0x000000003B3FFFFF + * Physical address: 0x0000000000000000 - 0x000000003B3FFFFF + */ + { .pbase = ALIGN_DOWN(0x0UL, __PAGE_SIZE), + .vbase = ALIGN_DOWN(0x0UL, __PAGE_SIZE), + .pg_off = 0x0, + .len = 0x3B400000UL, + .pg_count = PAGE_COUNT(0x3B400000UL), + .type = UKPLAT_MEMRT_FREE, + .flags = UKPLAT_MEMRF_VALUE_IS_WR | UKPLAT_MEMRF_UXN | + UKPLAT_MEMRF_MAIR(UKPLAT_MEMRF_MAIR_NORMAL_WB) | + UKPLAT_MEMRF_LEVEL(UKPLAT_MEMRF_LEVEL_PMD) +#ifdef CONFIG_UKPLAT_MEMRNAME + , + .name = { 0 } +#endif /* CONFIG_UKPLAT_MEMRNAME */ + }, + /* DEVICE + * Virtual address: 0x000000003F000000 - 0x000000003FFFFFFF + * Physical address: 0x000000003F000000 - 0x000000003FFFFFFF + */ + { .pbase = ALIGN_DOWN(0x3F000000UL, __PAGE_SIZE), + .vbase = ALIGN_DOWN(0x3F000000UL, __PAGE_SIZE), + .pg_off = 0x0, + .len = 0x1000000UL, + .pg_count = PAGE_COUNT(0x1000000UL), + .type = UKPLAT_MEMRT_RESERVED, + .flags = UKPLAT_MEMRF_VALUE_IS_WR | + UKPLAT_MEMRF_MAIR(UKPLAT_MEMRF_MAIR_DEVICE_nGnRnE) | + UKPLAT_MEMRF_LEVEL(UKPLAT_MEMRF_LEVEL_PMD) +#ifdef CONFIG_UKPLAT_MEMRNAME + , + .name = { 0 } +#endif /* CONFIG_UKPLAT_MEMRNAME */ + }, + /* DEVICE + * Virtual address: 0x0000000040000000 - 0x00000000401FFFFF + * Physical address: 0x0000000040000000 - 0x00000000401FFFFF + */ + { .pbase = ALIGN_DOWN(0x40000000UL, __PAGE_SIZE), + .vbase = ALIGN_DOWN(0x40000000UL, __PAGE_SIZE), + .pg_off = 0x0, + .len = 0x200000UL, + .pg_count = PAGE_COUNT(0x200000UL), + .type = UKPLAT_MEMRT_RESERVED, + .flags = UKPLAT_MEMRF_VALUE_IS_WR | + UKPLAT_MEMRF_MAIR(UKPLAT_MEMRF_MAIR_DEVICE_nGnRnE) | + UKPLAT_MEMRF_LEVEL(UKPLAT_MEMRF_LEVEL_PMD) +#ifdef CONFIG_UKPLAT_MEMRNAME + , + .name = { 0 } +#endif /* CONFIG_UKPLAT_MEMRNAME */ + }, + /* DEVICE BUS + * Virtual address: 0x000000007E000000 - 0x000000007EFFFFFF + * Physical address: 0x000000007E000000 - 0x000000007EFFFFFF + */ + { .pbase = ALIGN_DOWN(0x7E000000UL, __PAGE_SIZE), + .vbase = ALIGN_DOWN(0x7E000000UL, __PAGE_SIZE), + .pg_off = 0x0, + .len = 0x1000000UL, + .pg_count = PAGE_COUNT(0x1000000UL), + .type = UKPLAT_MEMRT_RESERVED, + .flags = UKPLAT_MEMRF_VALUE_IS_WR | + UKPLAT_MEMRF_MAIR(UKPLAT_MEMRF_MAIR_DEVICE_nGnRnE) | + UKPLAT_MEMRF_LEVEL(UKPLAT_MEMRF_LEVEL_PMD) +#ifdef CONFIG_UKPLAT_MEMRNAME + , + .name = { 0 } +#endif /* CONFIG_UKPLAT_MEMRNAME */ + } +}; + +#ifdef CONFIG_PAGING +struct ukplat_memregion_desc + direct_map_mrd __section(".tn_direct_memregion") = { + /* Direct-mapped + * Virtual address: 0x0000ff8000000000 - 0x0000ffffffffffff + * Physical address: 0x0000000000000000 - 0x0000008000000000 + */ + .pbase = ALIGN_DOWN(0x0UL, __PAGE_SIZE), + .vbase = ALIGN_DOWN(0xff8000000000UL, __PAGE_SIZE), + .pg_off = 0x0, + .len = 0x8000000000UL, + .pg_count = PAGE_COUNT(0x8000000000UL), + .type = UKPLAT_MEMRT_RESERVED, + .flags = UKPLAT_MEMRF_VALUE_IS_WR | + UKPLAT_MEMRF_MAIR(UKPLAT_MEMRF_MAIR_NORMAL_WB) | + UKPLAT_MEMRF_LEVEL(UKPLAT_MEMRF_LEVEL_PUD) +#ifdef CONFIG_UKPLAT_MEMRNAME + , + .name = { 0 } +#endif /* CONFIG_UKPLAT_MEMRNAME */ +}; +#endif + +__u32 bpt_memregion_count = + sizeof(bpt_memregion) / sizeof(struct ukplat_memregion_desc); + +#endif diff --git a/start.S b/start.S index 97d92cf..6cd0cab 100644 --- a/start.S +++ b/start.S @@ -18,7 +18,6 @@ #include #include #include -#include #define VA_START 0 #define BOOTSTACK_SIZE 4096 -- Gitee