Skip to content

Commit e7d870b

Browse files
unicornxRbb666
authored andcommitted
bsp: cvitek: riscv: use marco for linker script
Regarding link script, some memory offsets and sizes are different between the standard version and the smart version (such as "__STACKSIZE__", kernel start address, kernel memory size, etc.). Original solution is replacing link scripts and use INCLUDE, which is relatively complicated. This improvement uses macros to replace constants in the link script. The preprocessing mechanism provided by gcc is used to dynamically replace constant values during the build process. In addition, the kernel load address (the corresponding KERNEL_VADDR_START for smart) was originally configured as 0xFFFFFFC000200000, which is default value of riscv with kernelmap enabled (0xffffffc000000000) plus offset to skip over bootloader (0x200000). This caused a trouble: due to default bsp configuration is for smart, if we switched to the standard version and build, and then switched back to the smart version, the value of KERNEL_VADDR_START will be default back to 0xffffffc000000000, which is different from the original configuration value, resulting in the need to manually reconfigure it, which is easy to forget and cause problems. The current solution is to use the default value 0xffffffc000000000 in the configuration. Add offset to the code and link script when using it. This patch update the default .config and rtconfig.h for cv18xx_riscv. Signed-off-by: Chen Wang <[email protected]>
1 parent 7371a00 commit e7d870b

File tree

13 files changed

+106
-261
lines changed

13 files changed

+106
-261
lines changed

bsp/cvitek/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ RT-Thread Kernel --->
163163
[*] Enable RT-Thread Smart (microkernel on kernel/userland)
164164
```
165165

166-
**注意检查内核虚拟起始地址的配置,确保为 `0xFFFFFFC000200000`**
166+
**注意检查内核虚拟起始地址的配置,确保为 `0xFFFFFFC000000000`**
167167

168168
```shell
169169
RT-Thread Kernel --->
170-
(0xFFFFFFC000200000) The virtural address of kernel start
170+
(0xFFFFFFC000000000) The virtural address of kernel start
171171
RT-Thread Components --->
172172
```
173173

bsp/cvitek/cv18xx_risc-v/.config

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ CONFIG_RT_DEBUGING_ASSERT=y
155155
CONFIG_RT_DEBUGING_COLOR=y
156156
CONFIG_RT_DEBUGING_CONTEXT=y
157157
# CONFIG_RT_DEBUGING_AUTO_INIT is not set
158-
# CONFIG_RT_DEBUGING_PAGE_LEAK is not set
159158
# CONFIG_RT_USING_CI_ACTION is not set
160159

161160
#
@@ -173,7 +172,6 @@ CONFIG_RT_USING_MESSAGEQUEUE=y
173172
#
174173
# Memory Management
175174
#
176-
CONFIG_RT_PAGE_MAX_ORDER=11
177175
# CONFIG_RT_USING_MEMPOOL is not set
178176
# CONFIG_RT_USING_SMALL_MEM is not set
179177
CONFIG_RT_USING_SLAB=y
@@ -195,15 +193,15 @@ CONFIG_RT_USING_DEVICE_OPS=y
195193
CONFIG_RT_USING_CONSOLE=y
196194
CONFIG_RT_CONSOLEBUF_SIZE=256
197195
CONFIG_RT_CONSOLE_DEVICE_NAME="uart0"
198-
CONFIG_RT_VER_NUM=0x50200
196+
CONFIG_RT_VER_NUM=0x50201
199197
CONFIG_RT_USING_STDC_ATOMIC=y
200198
CONFIG_RT_BACKTRACE_LEVEL_MAX_NR=32
201199
# end of RT-Thread Kernel
202200

203201
CONFIG_ARCH_CPU_64BIT=y
204202
CONFIG_RT_USING_CACHE=y
205203
CONFIG_ARCH_MM_MMU=y
206-
CONFIG_KERNEL_VADDR_START=0xFFFFFFC000200000
204+
CONFIG_KERNEL_VADDR_START=0xffffffc000000000
207205
CONFIG_ARCH_RISCV=y
208206
CONFIG_ARCH_RISCV_FPU=y
209207
CONFIG_ARCH_RISCV_FPU_D=y
@@ -274,6 +272,7 @@ CONFIG_RT_DFS_ELM_MUTEX_TIMEOUT=3000
274272
CONFIG_RT_USING_DFS_DEVFS=y
275273
CONFIG_RT_USING_DFS_ROMFS=y
276274
CONFIG_RT_USING_DFS_PTYFS=y
275+
# CONFIG_RT_USING_DFS_PROCFS is not set
277276
# CONFIG_RT_USING_DFS_CROMFS is not set
278277
CONFIG_RT_USING_DFS_TMPFS=y
279278
# CONFIG_RT_USING_DFS_MQUEUE is not set
@@ -533,9 +532,25 @@ CONFIG_RT_USING_ADT_REF=y
533532
# end of Utilities
534533

535534
# CONFIG_RT_USING_VBUS is not set
535+
536+
#
537+
# Memory management
538+
#
539+
CONFIG_RT_PAGE_AFFINITY_BLOCK_SIZE=0x1000
540+
CONFIG_RT_PAGE_MAX_ORDER=11
541+
# CONFIG_RT_USING_MEMBLOCK is not set
542+
543+
#
544+
# Debugging
545+
#
546+
# CONFIG_RT_DEBUGGING_ALIASING is not set
547+
# CONFIG_RT_DEBUGING_PAGE_LEAK is not set
548+
# CONFIG_RT_DEBUGGING_PAGE_POISON is not set
549+
# end of Debugging
550+
# end of Memory management
551+
536552
CONFIG_RT_USING_LWP=y
537-
CONFIG_LWP_DEBUG=y
538-
# CONFIG_LWP_DEBUG_INIT is not set
553+
# CONFIG_LWP_DEBUG is not set
539554
CONFIG_LWP_USING_RUNTIME=y
540555
CONFIG_RT_LWP_MAX_NR=30
541556
CONFIG_LWP_TASK_STACK_SIZE=16384
@@ -548,12 +563,6 @@ CONFIG_RT_USING_LDSO=y
548563
CONFIG_LWP_USING_TERMINAL=y
549564
CONFIG_LWP_PTY_MAX_PARIS_LIMIT=64
550565

551-
#
552-
# Memory management
553-
#
554-
# CONFIG_RT_USING_MEMBLOCK is not set
555-
# end of Memory management
556-
557566
#
558567
# Using USB legacy version
559568
#
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
dtb/
2-
Image
2+
Image
3+
4+
link.lds.generated

bsp/cvitek/cv18xx_risc-v/SConstruct

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,11 @@ drivers_path_prefix = os.path.dirname(SDK_ROOT) + '/drivers'
2929
# prepare building environment
3030
objs = PrepareBuilding(env, RTT_ROOT, has_libcpu = False)
3131

32-
if GetDepend('RT_USING_SMART'):
33-
# use smart link.lds
34-
env['LINKFLAGS'] = env['LINKFLAGS'].replace('link.lds', 'link_smart.lds')
35-
3632
# include libraries
3733
objs.extend(SConscript(drivers_path_prefix + '/SConscript', variant_dir='build/drivers', duplicate=0))
3834

39-
stack_size = 4096
40-
41-
stack_lds = open('link_stacksize.lds', 'w')
42-
if GetDepend('__STACKSIZE__'):
43-
stack_size = GetDepend('__STACKSIZE__')
44-
stack_lds.write('__STACKSIZE__ = %d;\n' % stack_size)
45-
stack_lds.close()
35+
from gen_ld import *
36+
generate_ldscript('link.lds', 'link.lds.generated')
4637

4738
# make a building
4839
DoBuilding(TARGET, objs)

bsp/cvitek/cv18xx_risc-v/board/board.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,32 @@
1414

1515
#include "sbi.h"
1616

17+
extern unsigned int __sram_end;
18+
#define RAM_END (rt_size_t)((void *)&__sram_end)
19+
20+
extern unsigned int __bss_start;
21+
extern unsigned int __bss_end;
22+
23+
#define RT_HW_HEAP_BEGIN ((void *)&__bss_end)
24+
#define RT_HW_HEAP_END ((void *)((rt_size_t)RT_HW_HEAP_BEGIN + 8 * 1024 * 1024))
25+
26+
#define RT_HW_PAGE_START RT_HW_HEAP_END
27+
#define RT_HW_PAGE_END ((void *)(RAM_END))
28+
1729
#ifdef RT_USING_SMART
1830
#include "riscv_mmu.h"
1931
#include "mmu.h"
2032
#include "page.h"
2133
#include "lwp_arch.h"
2234

23-
/* respect to boot loader, must be 0xFFFFFFC000200000 */
24-
RT_STATIC_ASSERT(kmem_region, KERNEL_VADDR_START == 0xFFFFFFC000200000);
35+
#include "mem_layout.h"
2536

2637
rt_region_t init_page_region = {(rt_size_t)RT_HW_PAGE_START, (rt_size_t)RT_HW_PAGE_END};
2738

2839
extern size_t MMUTable[];
2940

3041
struct mem_desc platform_mem_desc[] = {
31-
{KERNEL_VADDR_START, (rt_size_t)RT_HW_PAGE_END - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
42+
{KERNEL_START, (rt_size_t)RT_HW_PAGE_END - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
3243
};
3344

3445
#define NUM_MEM_DESC (sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]))

bsp/cvitek/cv18xx_risc-v/board/board.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@
1515
#include "drv_uart.h"
1616
#include "tick.h"
1717

18-
extern unsigned int __sram_end;
19-
#define RAM_END (rt_size_t)((void *)&__sram_end)
20-
21-
extern unsigned int __bss_start;
22-
extern unsigned int __bss_end;
23-
24-
#define RT_HW_HEAP_BEGIN ((void *)&__bss_end)
25-
#define RT_HW_HEAP_END ((void *)((rt_size_t)RT_HW_HEAP_BEGIN + 8 * 1024 * 1024))
26-
27-
#define RT_HW_PAGE_START RT_HW_HEAP_END
28-
#define RT_HW_PAGE_END ((void *)(RAM_END))
29-
3018
void rt_hw_board_init(void);
3119

3220
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2006-2021, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef MEMORY_LAYOUT_H__
7+
#define MEMORY_LAYOUT_H__
8+
9+
#include "../rtconfig.h"
10+
11+
/* Real start address of kernel is located after bootloader */
12+
#define KERNEL_OFFSET 0x200000
13+
14+
#ifdef RT_USING_SMART
15+
#define KERNEL_START (KERNEL_VADDR_START + KERNEL_OFFSET)
16+
#define KERNEL_SIZE 64 * 1024 * 1024
17+
#else
18+
#define KERNEL_START (0x80000000 + KERNEL_OFFSET)
19+
#define KERNEL_SIZE 32 * 1024 * 1024
20+
#endif
21+
22+
#endif // MEMORY_LAYOUT_H__

bsp/cvitek/cv18xx_risc-v/gen_ld.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import rtconfig
3+
import platform
4+
import subprocess
5+
6+
def generate_ldscript(input, output):
7+
8+
if not os.path.exists(input):
9+
print('Error: file', input, 'not found')
10+
return
11+
12+
if os.path.exists(output):
13+
os.remove(output)
14+
15+
if rtconfig.PLATFORM == 'gcc':
16+
17+
gcc_cmd = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
18+
19+
# gcc -E -P -x c $input -o $output
20+
if (platform.system() == 'Windows'):
21+
child = subprocess.Popen([gcc_cmd, '-E', '-P', '-x', 'c', input, '-o', output], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
22+
else:
23+
child = subprocess.Popen(gcc_cmd + f' -E -P -x c {input} -o {output}', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
24+
25+
child.communicate()
26+
27+
print(output, 'is generated from', input)

bsp/cvitek/cv18xx_risc-v/link.lds

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,20 @@
88
* 2020/12/12 bernard The first version
99
*/
1010

11-
INCLUDE "link_stacksize.lds"
11+
#include "board/mem_layout.h"
1212

1313
OUTPUT_ARCH( "riscv" )
1414

15-
/*
16-
* Memory layout:
17-
* 0x10200000 - 0x10201000: Bootloader
18-
* 0x10201000 - 0x10A00000: Kernel
19-
* 0x10A00000 - 0x11200000: Heap
20-
*/
21-
2215
MEMORY
2316
{
24-
SRAM(wx): ORIGIN = 0x80200000, LENGTH = 32M
17+
SRAM(wx): ORIGIN = KERNEL_START, LENGTH = KERNEL_SIZE
2518
}
2619

2720
ENTRY(_start)
2821
SECTIONS
2922
{
3023
. = ORIGIN(SRAM) ;
3124

32-
/* __STACKSIZE__ = 4096; */
3325
__sram_base = ORIGIN(SRAM);
3426
__sram_size = LENGTH(SRAM);
3527
__sram_end = __sram_base + __sram_size;

0 commit comments

Comments
 (0)