Skip to content

Commit dc725d0

Browse files
committed
semihost: Add rudimentary support for openrisc
This has basic stdio support and _exit, with stdio using the 16550 uart and _exit using the sifive_test device. Signed-off-by: Keith Packard <[email protected]>
1 parent d5401cb commit dc725d0

File tree

5 files changed

+371
-0
lines changed

5 files changed

+371
-0
lines changed

semihost/machine/or1k/meson.build

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
#
4+
# Copyright © 2021 Keith Packard
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions
8+
# are met:
9+
#
10+
# 1. Redistributions of source code must retain the above copyright
11+
# notice, this list of conditions and the following disclaimer.
12+
#
13+
# 2. Redistributions in binary form must reproduce the above
14+
# copyright notice, this list of conditions and the following
15+
# disclaimer in the documentation and/or other materials provided
16+
# with the distribution.
17+
#
18+
# 3. Neither the name of the copyright holder nor the names of its
19+
# contributors may be used to endorse or promote products derived
20+
# from this software without specific prior written permission.
21+
#
22+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27+
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33+
# OF THE POSSIBILITY OF SUCH DAMAGE.
34+
#
35+
36+
lib_semihost_srcs = [
37+
'or1k_stub.c',
38+
'or1k_exit.c',
39+
'or1k_iob.c',
40+
]
41+
42+
has_semihost = true
43+
44+
foreach params : targets
45+
target = params['name']
46+
target_dir = params['dir']
47+
target_c_args = params['c_args']
48+
target_lib_prefix = params['lib_prefix']
49+
50+
instdir = join_paths(lib_dir, target_dir)
51+
52+
libsemihost_name = 'semihost'
53+
54+
local_lib_semihost_target = static_library(join_paths(target_dir, target_lib_prefix + libsemihost_name),
55+
lib_semihost_srcs,
56+
install : true,
57+
install_dir : instdir,
58+
include_directories : inc,
59+
c_args : target_c_args + c_args,
60+
pic: false)
61+
62+
set_variable('lib_semihost' + target, local_lib_semihost_target)
63+
64+
endforeach
65+

semihost/machine/or1k/or1k_exit.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2021 Keith Packard
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
*
18+
* 3. Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33+
* OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
36+
#include "or1k_semihost.h"
37+
38+
#define sifive_test (*((volatile uint32_t*) 0x96000000))
39+
#define FINISHER_FAIL 0x3333
40+
#define FINISHER_PASS 0x5555
41+
#define FINISHER_RESET 0x7777
42+
43+
_Noreturn void
44+
_exit(int code)
45+
{
46+
uint32_t val;
47+
48+
if (code == 0)
49+
val = FINISHER_PASS;
50+
else
51+
val = FINISHER_FAIL | (code << 16);
52+
sifive_test = val;
53+
while (1);
54+
}

semihost/machine/or1k/or1k_iob.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2023 Keith Packard
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
*
18+
* 3. Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33+
* OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
36+
#include "or1k_semihost.h"
37+
38+
typedef volatile uint8_t vuint8_t;
39+
40+
struct uart_16550 {
41+
vuint8_t data;
42+
vuint8_t ier;
43+
vuint8_t iir;
44+
vuint8_t lcr;
45+
46+
vuint8_t mcr;
47+
vuint8_t lsr;
48+
vuint8_t msr;
49+
vuint8_t scr;
50+
};
51+
52+
/* openrisc virt serial port */
53+
#define uart (*((struct uart_16550 *) 0x90000000))
54+
55+
#define UART_LSR_FIFOE 0x80 /* Fifo error */
56+
#define UART_LSR_TEMT 0x40 /* Transmitter empty */
57+
#define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
58+
#define UART_LSR_BI 0x10 /* Break interrupt indicator */
59+
#define UART_LSR_FE 0x08 /* Frame error indicator */
60+
#define UART_LSR_PE 0x04 /* Parity error indicator */
61+
#define UART_LSR_OE 0x02 /* Overrun error indicator */
62+
#define UART_LSR_DR 0x01 /* Receiver data ready */
63+
64+
int
65+
or1k_putc(char c, FILE *file)
66+
{
67+
(void) file;
68+
while ((uart.lsr & (UART_LSR_TEMT|UART_LSR_THRE)) != (UART_LSR_TEMT|UART_LSR_THRE))
69+
;
70+
uart.data = (uint8_t) c;
71+
return (unsigned char) c;
72+
}
73+
74+
#ifdef TINY_STDIO
75+
76+
static int
77+
or1k_getc(FILE *file)
78+
{
79+
(void) file;
80+
return EOF;
81+
}
82+
83+
static FILE __stdio = FDEV_SETUP_STREAM(or1k_putc, or1k_getc, NULL, _FDEV_SETUP_RW);
84+
85+
#ifdef __strong_reference
86+
#define STDIO_ALIAS(x) __strong_reference(stdin, x);
87+
#else
88+
#define STDIO_ALIAS(x) FILE *const x = &__stdio;
89+
#endif
90+
91+
FILE *const stdin = &__stdio;
92+
STDIO_ALIAS(stdout);
93+
STDIO_ALIAS(stderr);
94+
95+
#endif
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2023 Keith Packard
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
*
18+
* 3. Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33+
* OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
36+
#ifndef _OR1K_SEMIHOST_H_
37+
#define _OR1K_SEMIHOST_H_
38+
39+
#include <stdio.h>
40+
#include <stdlib.h>
41+
#include <stdint.h>
42+
#include <unistd.h>
43+
#include <fcntl.h>
44+
#include <sys/stat.h>
45+
46+
int
47+
or1k_putc(char c, FILE *file);
48+
49+
#endif /* _OR1K_SEMIHOST_H_ */

semihost/machine/or1k/or1k_stub.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2021 Keith Packard
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
*
18+
* 3. Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33+
* OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
36+
#define _GNU_SOURCE
37+
#include "or1k_semihost.h"
38+
39+
ssize_t
40+
read(int fd, void *buf, size_t count)
41+
{
42+
(void) fd;
43+
(void) buf;
44+
(void) count;
45+
return 0;
46+
}
47+
48+
ssize_t
49+
write(int fd, const void *buf, size_t count)
50+
{
51+
const char *b = buf;
52+
size_t c = count;
53+
54+
(void) fd;
55+
while (c--)
56+
or1k_putc(*b++, NULL);
57+
return count;
58+
}
59+
60+
int
61+
open(const char *pathname, int flags, ...)
62+
{
63+
(void) pathname;
64+
(void) flags;
65+
return -1;
66+
}
67+
68+
int
69+
close(int fd)
70+
{
71+
(void) fd;
72+
return 0;
73+
}
74+
75+
off_t lseek(int fd, off_t offset, int whence)
76+
{
77+
(void) fd;
78+
(void) offset;
79+
(void) whence;
80+
return (off_t) -1;
81+
}
82+
83+
_off64_t lseek64(int fd, _off64_t offset, int whence)
84+
{
85+
return (_off64_t) lseek(fd, (off_t) offset, whence);
86+
}
87+
88+
int
89+
unlink(const char *pathname)
90+
{
91+
(void) pathname;
92+
return 0;
93+
}
94+
95+
int
96+
fstat (int fd, struct stat *sbuf)
97+
{
98+
(void) fd;
99+
(void) sbuf;
100+
return -1;
101+
}
102+
103+
int
104+
isatty (int fd)
105+
{
106+
(void) fd;
107+
return 1;
108+
}

0 commit comments

Comments
 (0)