Skip to content

Commit f3c1303

Browse files
FlyGoatkeith-packard
authored andcommitted
semihost: Implement UHI semihosting for MIPS
UHI is the semihosting protocol defined by MIPS. It has been implemented in CodeScape and QEMU. Signed-off-by: Jiaxun Yang <[email protected]>
1 parent fce733e commit f3c1303

File tree

12 files changed

+785
-201
lines changed

12 files changed

+785
-201
lines changed

semihost/machine/mips/meson.build

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33
#
44
# Copyright © 2021 Keith Packard
5+
# Copyright © 2025 Jiaxun Yang <[email protected]>
56
#
67
# Redistribution and use in source and binary forms, with or without
78
# modification, are permitted provided that the following conditions
@@ -32,12 +33,17 @@
3233
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3334
# OF THE POSSIBILITY OF SUCH DAMAGE.
3435
#
35-
3636
lib_semihost_srcs = [
37-
'mips_stub.c',
37+
'mips_close.c',
3838
'mips_exit.c',
39-
'mips_iob.c',
40-
]
39+
'mips_fstat.c',
40+
'mips_lseek.c',
41+
'mips_open.c',
42+
'mips_read.c',
43+
'mips_stub.c',
44+
'mips_unlink.c',
45+
'mips_write.c',
46+
]
4147

4248
has_semihost = true
4349

@@ -62,4 +68,3 @@ foreach params : targets
6268
set_variable('lib_semihost' + target, local_lib_semihost_target)
6369

6470
endforeach
65-

semihost/machine/mips/mips_close.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2025 Jiaxun Yang <[email protected]>
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 "mips_semihost.h"
37+
#include <sys/cdefs.h>
38+
#include <unistd.h>
39+
40+
int
41+
close(int fd)
42+
{
43+
return mips_semihost1(SYS_SEMIHOST_close, fd);
44+
}

semihost/machine/mips/mips_exit.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: BSD-3-Clause
33
*
4-
* Copyright © 2021 Keith Packard
4+
* Copyright © 2025 Jiaxun Yang <[email protected]>
55
*
66
* Redistribution and use in source and binary forms, with or without
77
* modification, are permitted provided that the following conditions
@@ -33,15 +33,13 @@
3333
* OF THE POSSIBILITY OF SUCH DAMAGE.
3434
*/
3535

36-
#include "mips-semihost.h"
36+
#include "mips_semihost.h"
37+
#include <sys/cdefs.h>
38+
#include <unistd.h>
3739

3840
_Noreturn void
3941
_exit(int code)
4042
{
41-
char buf[15];
42-
int n;
43-
/* Can't use printf because stdout has already been cleaned up */
44-
n = snprintf(buf, sizeof(buf), "%cexit %d\n", 0xe9, code);
45-
write(1, buf, n);
46-
while(1);
43+
mips_semihost1(SYS_SEMIHOST_exit, code);
44+
__unreachable();
4745
}

semihost/machine/mips/mips_fstat.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2025 Jiaxun Yang <[email protected]>
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+
#ifdef __GNUC__
37+
#pragma GCC diagnostic ignored "-Wpragmas"
38+
/* False positive for mips_stat */
39+
#pragma GCC diagnostic ignored "-Wanalyzer-use-of-uninitialized-value"
40+
#endif
41+
42+
#include "mips_semihost.h"
43+
#include <fcntl.h>
44+
#include <unistd.h>
45+
#include <string.h>
46+
#include <errno.h>
47+
#include <stdarg.h>
48+
49+
int
50+
fstat(int fd, struct stat *restrict statbuf)
51+
{
52+
struct mips_stat mips_stat;
53+
int ret;
54+
55+
ret = mips_semihost2(SYS_SEMIHOST_fstat, fd, (uintptr_t)&mips_stat);
56+
if (ret >= 0)
57+
copy_stat(statbuf, &mips_stat);
58+
59+
return ret;
60+
}

semihost/machine/mips/mips_iob.c

Lines changed: 0 additions & 122 deletions
This file was deleted.

semihost/machine/mips/mips_lseek.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2025 Jiaxun Yang <[email protected]>
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 "mips_semihost.h"
37+
#include <sys/types.h>
38+
#include <sys/stat.h>
39+
#include <fcntl.h>
40+
#include <unistd.h>
41+
#include <string.h>
42+
#include <errno.h>
43+
44+
off_t
45+
lseek(int fd, off_t offset, int whence)
46+
{
47+
off_t ret = mips_semihost3(SYS_SEMIHOST_lseek, fd, offset, whence);
48+
49+
return ret;
50+
}

0 commit comments

Comments
 (0)