Skip to content

Commit da08c5e

Browse files
xypronandreabolognani
authored andcommitted
Initial RISC-V support
Add what is needed to build on riscv64. Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com> This is an update to rhboot#420 which brings it in alignment with the current upstream. Signed-off-by: Brian 'redbeard' Harrington <redbeard@dead-city.org>
1 parent 0166495 commit da08c5e

File tree

11 files changed

+168
-2
lines changed

11 files changed

+168
-2
lines changed

Cryptlib/Include/OpenSslSupport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
5858

5959
#define CONFIG_HEADER_BN_H
6060

61-
#if defined(MDE_CPU_X64) || defined(MDE_CPU_AARCH64) || defined(MDE_CPU_IA64)
61+
#if defined(MDE_CPU_X64) || defined(MDE_CPU_AARCH64) || \
62+
defined(MDE_CPU_IA64) || defined(MDE_CPU_RISCV64)
6263
//
6364
// With GCC we would normally use SIXTY_FOUR_BIT_LONG, but MSVC needs
6465
// SIXTY_FOUR_BIT, because 'long' is 32-bit and only 'long long' is

Cryptlib/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ endif
4040
ifeq ($(ARCH),arm)
4141
DEFINES += -DMDE_CPU_ARM
4242
endif
43+
ifeq ($(ARCH),riscv64)
44+
DEFINES += -DMDE_CPU_RISCV64
45+
endif
4346

4447
LDFLAGS = -nostdlib -znocombreloc
4548

Cryptlib/OpenSSL/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ endif
5252
ifeq ($(ARCH),arm)
5353
DEFINES += -DMDE_CPU_ARM
5454
endif
55+
ifeq ($(ARCH),riscv64)
56+
DEFINES += -DMDE_CPU_RISCV64
57+
endif
5558

5659
LDFLAGS = -nostdlib -znocombreloc
5760

Make.defaults

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ ifeq ($(ARCH),arm)
9797
SUBSYSTEM := 0xa
9898
ARCH_LDFLAGS += --defsym=EFI_SUBSYSTEM=$(SUBSYSTEM)
9999
endif
100+
ifeq ($(ARCH),riscv64)
101+
ARCH_CFLAGS ?= -DMDE_CPU_RISCV64 -DPAGE_SIZE=4096
102+
ARCH_GNUEFI ?= riscv64
103+
ARCH_SUFFIX ?= riscv64
104+
ARCH_SUFFIX_UPPER ?= RISCV64
105+
FORMAT := -O binary
106+
SUBSYSTEM := 0xa
107+
ARCH_LDFLAGS += --defsym=EFI_SUBSYSTEM=$(SUBSYSTEM)
108+
TIMESTAMP_LOCATION := 72
109+
endif
100110
101111
DEFINES = -DDEFAULT_LOADER='L"$(DEFAULT_LOADER)"' \
102112
-DDEFAULT_LOADER_CHAR='"$(DEFAULT_LOADER)"'

elf_riscv64_efi.lds

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
OUTPUT_FORMAT("elf64-littleriscv", "elf64-littleriscv", "elf64-littleriscv")
2+
OUTPUT_ARCH(riscv)
3+
ENTRY(_start)
4+
SECTIONS
5+
{
6+
.text 0x0 : {
7+
_text = .;
8+
*(.text.head)
9+
*(.text)
10+
*(.text.*)
11+
*(.gnu.linkonce.t.*)
12+
_evtext = .;
13+
. = ALIGN(4096);
14+
}
15+
_etext = .;
16+
_text_size = . - _text;
17+
_text_vsize = _evtext - _text;
18+
19+
. = ALIGN(4096);
20+
.data :
21+
{
22+
_data = .;
23+
*(.sdata)
24+
*(.data)
25+
*(.data1)
26+
*(.data.*)
27+
*(.got.plt)
28+
*(.got)
29+
30+
*(.dynamic)
31+
32+
/* the EFI loader doesn't seem to like a .bss section, so we stick
33+
it all into .data: */
34+
. = ALIGN(16);
35+
_bss = .;
36+
*(.sbss)
37+
*(.scommon)
38+
*(.dynbss)
39+
*(.bss)
40+
*(COMMON)
41+
_evdata = .;
42+
. = ALIGN(4096);
43+
_bss_end = .;
44+
}
45+
_edata = .;
46+
_data_vsize = _evdata - _data;
47+
_data_size = . - _data;
48+
49+
/*
50+
* Note that _sbat must be the beginning of the data, and _esbat must be the
51+
* end and must be before any section padding. The sbat self-check uses
52+
* _esbat to find the bounds of the data, and if the padding is included, the
53+
* CSV parser (correctly) rejects the data as having NUL values in one of the
54+
* required columns.
55+
*/
56+
. = ALIGN(4096);
57+
.sbat :
58+
{
59+
_sbat = .;
60+
*(.sbat)
61+
*(.sbat.*)
62+
_esbat = .;
63+
. = ALIGN(4096);
64+
_epsbat = .;
65+
}
66+
_sbat_size = _epsbat - _sbat;
67+
_sbat_vsize = _esbat - _sbat;
68+
69+
. = ALIGN(4096);
70+
.rodata :
71+
{
72+
_rodata = .;
73+
*(.rodata*)
74+
*(.srodata)
75+
. = ALIGN(16);
76+
*(.note.gnu.build-id)
77+
. = ALIGN(4096);
78+
*(.vendor_cert)
79+
*(.data.ident)
80+
. = ALIGN(4096);
81+
}
82+
. = ALIGN(4096);
83+
.rela :
84+
{
85+
*(.rela.dyn)
86+
*(.rela.plt)
87+
*(.rela.got)
88+
*(.rela.data)
89+
*(.rela.data*)
90+
}
91+
. = ALIGN(4096);
92+
.dyn :
93+
{
94+
*(.dynsym)
95+
*(.dynstr)
96+
_evrodata = .;
97+
. = ALIGN(4096);
98+
}
99+
_erodata = .;
100+
_rodata_size = . - _rodata;
101+
_rodata_vsize = _evrodata - _rodata;
102+
_alldata_size = . - _data;
103+
104+
/DISCARD/ :
105+
{
106+
*(.rel.reloc)
107+
*(.eh_frame)
108+
*(.note.GNU-stack)
109+
}
110+
.comment 0 : { *(.comment) }
111+
}

include/asm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ static inline uint64_t read_counter(void)
1919
__asm__ __volatile__ ("mrs %0, pmccntr_el0" : "=r" (val));
2020
#elif defined(__arm__)
2121
__asm__ __volatile__ ("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
22+
#elif defined(__riscv) && __riscv_xlen == 64
23+
__asm__ __volatile__ ("csrr %0, 0xc01" : "=r" (val) : : "memory");
2224
#else
2325
#error unsupported arch
2426
#endif

include/peimage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
#define IMAGE_FILE_MACHINE_X64 0x8664
5151
#define IMAGE_FILE_MACHINE_ARMTHUMB_MIXED 0x01c2
5252
#define IMAGE_FILE_MACHINE_ARM64 0xaa64
53+
#define IMAGE_FILE_MACHINE_RISCV32 0x5032
54+
#define IMAGE_FILE_MACHINE_RISCV64 0x5064
5355

5456
//
5557
// EXE file formats

include/system/stdarg.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,23 @@ typedef __builtin_va_list __builtin_sysv_va_list;
2020
#endif
2121

2222
#if defined(__aarch64__) || defined(__arm__) || defined(__i386__) || \
23-
defined(__i486__) || defined(__i686__) || defined(__COVERITY__)
23+
defined(__i486__) || defined(__i686__) || defined(__COVERITY__) || defined(__riscv)
2424

2525
typedef __builtin_va_list sysv_va_list;
2626
#define sysv_va_copy(dest, start) __builtin_va_copy(dest, start)
2727
#define sysv_va_start(marker, arg) __builtin_va_start(marker, arg)
2828
#define sysv_va_arg(marker, type) __builtin_va_arg(marker, type)
2929
#define sysv_va_end(marker) __builtin_va_end(marker)
30+
31+
/*
32+
* gnu-efi needs this.
33+
*/
34+
typedef __builtin_va_list va_list;
35+
# define va_start(v,l) __builtin_va_start(v,l)
36+
# define va_end(v) __builtin_va_end(v)
37+
# define va_arg(v,l) __builtin_va_arg(v,l)
38+
# define va_copy(d,s) __builtin_va_copy(d,s)
39+
3040
/*
3141
* OpenSSL's X509ConstructCertificateStack needs this.
3242
*/

lib/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ endif
3030
ifeq ($(ARCH),arm)
3131
DEFINES += -DMDE_CPU_ARM
3232
endif
33+
ifeq ($(ARCH),riscv64)
34+
DEFINES += -DMDE_CPU_RISCV64
35+
endif
3336

3437
LDFLAGS = -nostdlib -znocombreloc
3538

pe-relocate.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ allow_64_bit(void)
280280
if (in_protocol)
281281
return 1;
282282
return 0;
283+
#elif defined (__riscv) && __riscv_xlen == 64
284+
return 1;
283285
#else /* assuming everything else is 32-bit... */
284286
return 0;
285287
#endif
@@ -300,6 +302,8 @@ allow_32_bit(void)
300302
return 1;
301303
#elif defined(__aarch64__)
302304
return 0;
305+
#elif defined (__riscv) && __riscv_xlen == 64
306+
return 0;
303307
#else /* assuming everything else is 32-bit... */
304308
return 1;
305309
#endif
@@ -326,6 +330,8 @@ static const UINT16 machine_type =
326330
IMAGE_FILE_MACHINE_I386;
327331
#elif defined(__ia64__)
328332
IMAGE_FILE_MACHINE_IA64;
333+
#elif defined(__riscv) && __riscv_xlen == 64
334+
IMAGE_FILE_MACHINE_RISCV64;
329335
#else
330336
#error this architecture is not supported by shim
331337
#endif

0 commit comments

Comments
 (0)