Skip to content

Commit 5031a99

Browse files
xypronbrianredbeard
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 #420 which brings it in alignment with the current upstream. Signed-off-by: Brian 'redbeard' Harrington <redbeard@dead-city.org>
1 parent 5914984 commit 5031a99

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
@@ -61,7 +61,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
6161

6262
#define CONFIG_HEADER_BN_H
6363

64-
#if defined(MDE_CPU_X64) || defined(MDE_CPU_AARCH64) || defined(MDE_CPU_IA64)
64+
#if defined(MDE_CPU_X64) || defined(MDE_CPU_AARCH64) || \
65+
defined(MDE_CPU_IA64) || defined(MDE_CPU_RISCV64)
6566
//
6667
// With GCC we would normally use SIXTY_FOUR_BIT_LONG, but MSVC needs
6768
// 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
@@ -37,6 +37,9 @@ endif
3737
ifeq ($(ARCH),arm)
3838
DEFINES += -DMDE_CPU_ARM
3939
endif
40+
ifeq ($(ARCH),riscv64)
41+
DEFINES += -DMDE_CPU_RISCV64
42+
endif
4043

4144
LDFLAGS = -nostdlib -znocombreloc
4245

Cryptlib/OpenSSL/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ endif
4949
ifeq ($(ARCH),arm)
5050
DEFINES += -DMDE_CPU_ARM
5151
endif
52+
ifeq ($(ARCH),riscv64)
53+
DEFINES += -DMDE_CPU_RISCV64
54+
endif
5255

5356
LDFLAGS = -nostdlib -znocombreloc
5457

Make.defaults

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ ifeq ($(ARCH),arm)
9696
SUBSYSTEM := 0xa
9797
ARCH_LDFLAGS += --defsym=EFI_SUBSYSTEM=$(SUBSYSTEM)
9898
endif
99+
ifeq ($(ARCH),riscv64)
100+
ARCH_CFLAGS ?= -DMDE_CPU_RISCV64 -DPAGE_SIZE=4096
101+
ARCH_GNUEFI ?= riscv64
102+
ARCH_SUFFIX ?= riscv64
103+
ARCH_SUFFIX_UPPER ?= RISCV64
104+
FORMAT := -O binary
105+
SUBSYSTEM := 0xa
106+
ARCH_LDFLAGS += --defsym=EFI_SUBSYSTEM=$(SUBSYSTEM)
107+
TIMESTAMP_LOCATION := 72
108+
endif
99109
100110
DEFINES = -DDEFAULT_LOADER='L"$(DEFAULT_LOADER)"' \
101111
-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
@@ -24,7 +24,7 @@ typedef __builtin_va_list __builtin_sysv_va_list;
2424
#endif
2525

2626
#if defined(__aarch64__) || defined(__arm__) || defined(__i386__) || \
27-
defined(__i486__) || defined(__i686__) || defined(__COVERITY__)
27+
defined(__i486__) || defined(__i686__) || defined(__COVERITY__) || defined(__riscv)
2828

2929
typedef __builtin_va_list ms_va_list;
3030
typedef __builtin_va_list __builtin_ms_va_list;
@@ -38,6 +38,16 @@ typedef __builtin_va_list sysv_va_list;
3838
#define sysv_va_start(marker, arg) __builtin_va_start(marker, arg)
3939
#define sysv_va_arg(marker, type) __builtin_va_arg(marker, type)
4040
#define sysv_va_end(marker) __builtin_va_end(marker)
41+
42+
/*
43+
* gnu-efi needs this.
44+
*/
45+
typedef __builtin_va_list va_list;
46+
# define va_start(v,l) __builtin_va_start(v,l)
47+
# define va_end(v) __builtin_va_end(v)
48+
# define va_arg(v,l) __builtin_va_arg(v,l)
49+
# define va_copy(d,s) __builtin_va_copy(d,s)
50+
4151
/*
4252
* OpenSSL's X509ConstructCertificateStack needs this.
4353
*/

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)