Skip to content

Commit

Permalink
sysdeps/linux: port to m68k
Browse files Browse the repository at this point in the history
  • Loading branch information
netbsduser committed Oct 13, 2024
1 parent 151bbd5 commit dfc397a
Show file tree
Hide file tree
Showing 14 changed files with 803 additions and 1 deletion.
90 changes: 90 additions & 0 deletions abis/linux/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,96 @@ typedef struct __ucontext {
mcontext_t uc_mcontext;
} ucontext_t;

#elif defined (__m68k__)

// taken from musl

#if defined(_GNU_SOURCE) || defined(__MLIBC_BUILDING_MLIBC)
enum { R_D0 = 0 };
#define R_D0 R_D0
enum { R_D1 = 1 };
#define R_D1 R_D1
enum { R_D2 = 2 };
#define R_D2 R_D2
enum { R_D3 = 3 };
#define R_D3 R_D3
enum { R_D4 = 4 };
#define R_D4 R_D4
enum { R_D5 = 5 };
#define R_D5 R_D5
enum { R_D6 = 6 };
#define R_D6 R_D6
enum { R_D7 = 7 };
#define R_D7 R_D7
enum { R_A0 = 8 };
#define R_A0 R_A0
enum { R_A1 = 9 };
#define R_A1 R_A1
enum { R_A2 = 10 };
#define R_A2 R_A2
enum { R_A3 = 11 };
#define R_A3 R_A3
enum { R_A4 = 12 };
#define R_A4 R_A4
enum { R_A5 = 13 };
#define R_A5 R_A5
enum { R_A6 = 14 };
#define R_A6 R_A6
enum { R_A7 = 15 };
#define R_A7 R_A7
enum { R_SP = 15 };
#define R_SP R_SP
enum { R_PC = 16 };
#define R_PC R_PC
enum { R_PS = 17 };
#define R_PS R_PS
#endif

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(__MLIBC_BUILDING_MLIBC)

struct sigcontext {
unsigned long sc_mask, sc_usp, sc_d0, sc_d1, sc_a0, sc_a1;
unsigned short sc_sr;
unsigned long sc_pc;
unsigned short sc_formatvec;
unsigned long sc_fpregs[6], sc_fpcntl[3];
unsigned char sc_fpstate[216];
};

typedef int greg_t, gregset_t[18];
typedef struct {
int f_pcr, f_psr, f_fpiaddr, f_fpregs[8][3];
} fpregset_t;

typedef struct {
int version;
gregset_t gregs;
fpregset_t fpregs;
} mcontext_t;
#else
typedef struct {
int __version;
int __gregs[18];
int __fpregs[27];
} mcontext_t;
#endif

struct sigaltstack {
void *ss_sp;
int ss_flags;
size_t ss_size;
};

typedef struct __ucontext {
unsigned long uc_flags;
struct __ucontext *uc_link;
stack_t uc_stack;
mcontext_t uc_mcontext;
long __reserved[80];
sigset_t uc_sigmask;
} ucontext_t;


#else
#error "Missing architecture specific code."
#endif
Expand Down
21 changes: 21 additions & 0 deletions abis/linux/stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ struct stat {
struct timespec st_mtim;
struct timespec st_ctim;
};
#elif defined (__m68k__)

struct stat {
unsigned long long st_dev;
unsigned char __st_dev_padding[2];
unsigned long __st_ino;
unsigned int st_mode;
unsigned int st_nlink;
unsigned long st_uid;
unsigned long st_gid;
unsigned long long st_rdev;
unsigned char __st_rdev_padding;
long long st_size;
unsigned long st_blksize;
unsigned long long st_blocks;
struct timespec st_atim;
struct timespec st_mtim;
struct timespec st_ctim;
unsigned long long st_ino;
};


#endif

Expand Down
13 changes: 13 additions & 0 deletions ci/linux-m68k-gcc.cross-file
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[binaries]
c = 'm68k-linux-mlibc-gcc'
cpp = 'm68k-linux-mlibc-g++'
exe_wrapper = 'qemu-m68k'

[host_machine]
system = 'linux'
cpu_family = 'm68k'
cpu = 'm68k'
endian = 'big'

[properties]
skip_sanity_check = true
13 changes: 13 additions & 0 deletions sysdeps/linux/generic/sysdeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ struct user_desc {

#endif

#if defined(__m68k__)
extern "C" void *__m68k_read_tp() {
auto ret = do_syscall(__NR_get_thread_area, 0);
return (void *)ret;
}
#endif

int sys_tcb_set(void *pointer) {
#if defined(__x86_64__)
auto ret = do_syscall(SYS_arch_prctl, 0x1002 /* ARCH_SET_FS */, pointer);
Expand All @@ -93,6 +100,10 @@ int sys_tcb_set(void *pointer) {
#elif defined (__aarch64__)
uintptr_t thread_data = reinterpret_cast<uintptr_t>(pointer) + sizeof(Tcb) - 0x10;
asm volatile ("msr tpidr_el0, %0" :: "r"(thread_data));
#elif defined (__m68k__)
auto ret = do_syscall(__NR_set_thread_area, (uintptr_t)pointer + 0x7000 + sizeof(Tcb));
if(int e = sc_error(ret); e)
return e;
#else
#error "Missing architecture specific code."
#endif
Expand Down Expand Up @@ -658,6 +669,8 @@ int sys_before_cancellable_syscall(ucontext_t *uct) {
auto pc = reinterpret_cast<void*>(uct->uc_mcontext.gregs[REG_PC]);
#elif defined(__aarch64__)
auto pc = reinterpret_cast<void*>(uct->uc_mcontext.pc);
#elif defined(__m68k__)
auto pc = reinterpret_cast<void*>(uct->uc_mcontext.gregs[R_PC]);
#else
#error "Missing architecture specific code."
#endif
Expand Down
117 changes: 117 additions & 0 deletions sysdeps/linux/m68k/arch-syscall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <sys/syscall.h>
#include <bits/syscall.h>

using sc_word_t = __sc_word_t;

sc_word_t __do_syscall0(long sc) {
register int sc_reg asm("d0") = sc;
register sc_word_t ret asm("d0");
asm volatile ("trap #0" : "=r"(ret) : "r"(sc_reg) : "memory");
return ret;
}

sc_word_t __do_syscall1(long sc,
sc_word_t arg1) {
register int sc_reg asm("d0") = sc;
register sc_word_t arg1_reg asm("d1") = arg1;
register sc_word_t ret asm("d0");
asm volatile ("trap #0" : "=r"(ret) :
"r"(sc_reg),
"r"(arg1_reg)
: "memory");
return ret;
}

sc_word_t __do_syscall2(long sc,
sc_word_t arg1, sc_word_t arg2) {
register int sc_reg asm("d0") = sc;
register sc_word_t arg1_reg asm("d1") = arg1;
register sc_word_t arg2_reg asm("d2") = arg2;
register sc_word_t ret asm("d0");
asm volatile ("trap #0" : "=r"(ret) :
"r"(sc_reg),
"r"(arg1_reg),
"r"(arg2_reg)
: "memory");
return ret;
}

sc_word_t __do_syscall3(long sc,
sc_word_t arg1, sc_word_t arg2, sc_word_t arg3) {
register int sc_reg asm("d0") = sc;
register sc_word_t arg1_reg asm("d1") = arg1;
register sc_word_t arg2_reg asm("d2") = arg2;
register sc_word_t arg3_reg asm("d3") = arg3;
register sc_word_t ret asm("d0");
asm volatile ("trap #0" : "=r"(ret) :
"r"(sc_reg),
"r"(arg1_reg),
"r"(arg2_reg),
"r"(arg3_reg)
: "memory");
return ret;
}

sc_word_t __do_syscall4(long sc,
sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
sc_word_t arg4) {
register int sc_reg asm("d0") = sc;
register sc_word_t arg1_reg asm("d1") = arg1;
register sc_word_t arg2_reg asm("d2") = arg2;
register sc_word_t arg3_reg asm("d3") = arg3;
register sc_word_t arg4_reg asm("d4") = arg4;
register sc_word_t ret asm("d0");
asm volatile ("trap #0" : "=r"(ret) :
"r"(sc_reg),
"r"(arg1_reg),
"r"(arg2_reg),
"r"(arg3_reg),
"r"(arg4_reg)
: "memory");
return ret;
}

sc_word_t __do_syscall5(long sc,
sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
sc_word_t arg4, sc_word_t arg5) {
register int sc_reg asm("d0") = sc;
register sc_word_t arg1_reg asm("d1") = arg1;
register sc_word_t arg2_reg asm("d2") = arg2;
register sc_word_t arg3_reg asm("d3") = arg3;
register sc_word_t arg4_reg asm("d4") = arg4;
register sc_word_t arg5_reg asm("d5") = arg5;
register sc_word_t ret asm("d0");
asm volatile ("trap #0" : "=r"(ret) :
"r"(sc_reg),
"r"(arg1_reg),
"r"(arg2_reg),
"r"(arg3_reg),
"r"(arg4_reg),
"r"(arg5_reg)
: "memory");
return ret;
}

sc_word_t __do_syscall6(long sc,
sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
sc_word_t arg4, sc_word_t arg5, sc_word_t arg6) {
register int sc_reg asm("d0") = sc;
register sc_word_t arg1_reg asm("d1") = arg1;
register sc_word_t arg2_reg asm("d2") = arg2;
register sc_word_t arg3_reg asm("d3") = arg3;
register sc_word_t arg4_reg asm("d4") = arg4;
register sc_word_t arg5_reg asm("d5") = arg5;
register sc_word_t arg6_reg asm("a0") = arg6;
register sc_word_t ret asm("d0");
asm volatile ("trap #0" : "=r"(ret) :
"r"(sc_reg),
"r"(arg1_reg),
"r"(arg2_reg),
"r"(arg3_reg),
"r"(arg4_reg),
"r"(arg5_reg),
"r"(arg6_reg)
: "memory"
);
return ret;
}
28 changes: 28 additions & 0 deletions sysdeps/linux/m68k/cp_syscall.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

.section .text
.global __mlibc_do_asm_cp_syscall
.global __mlibc_syscall_begin
.global __mlibc_syscall_end
.type __mlibc_do_asm_cp_syscall, "function"
__mlibc_do_asm_cp_syscall:
movem.l %d2-%d5,-(%sp)
jbsr __m68k_read_tp@PLTPC
/* cancelBits is at TP - 0x7030; LSB at -0x702d */
move.b -0x702d(%a0), %d0
__mlibc_syscall_begin:
/* tcbCancelEnableBit && tcbCancelTriggerBit */
andi.b #0x5, %d0
cmpi.b #0x5, %d0
beq cancel
movem.l 20(%sp),%d0-%d5/%a0
trap #0
__mlibc_syscall_end:
movem.l (%sp)+,%d2-%d5
rts

cancel:
movem.l (%sp)+,%d2-%d5
jbsr __mlibc_do_cancel@PLTPC
illegal

.section .note.GNU-stack,"",%progbits
15 changes: 15 additions & 0 deletions sysdeps/linux/m68k/crt-src/Scrt1.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.section .text
.global _start

.type _start, %function
.type main, %function
.type __mlibc_entry, %function

_start:
suba.l %fp, %fp
lea _GLOBAL_OFFSET_TABLE_@GOTPC (%pc), %a5
move.l main@GOT(%a5), -(%sp)
move.l %sp, -(%sp)
jbsr __mlibc_entry@PLTPC

.section .note.GNU-stack,"",%progbits
14 changes: 14 additions & 0 deletions sysdeps/linux/m68k/crt-src/crt1.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.section .text
.global _start

.type _start, %function
.type main, %function
.type __mlibc_entry, %function

_start:
suba.l %fp, %fp
move.l #main, -(%sp)
move.l %sp, -(%sp)
jsr __mlibc_entry

.section .note.GNU-stack,"",%progbits
9 changes: 9 additions & 0 deletions sysdeps/linux/m68k/crt-src/crti.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.section ".init"
.global _init
_init:

.section ".fini"
.globl _fini
_fini:

.section .note.GNU-stack,"",%progbits
7 changes: 7 additions & 0 deletions sysdeps/linux/m68k/crt-src/crtn.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.section .init
rts

.section .fini
rts

.section .note.GNU-stack,"",%progbits
19 changes: 19 additions & 0 deletions sysdeps/linux/m68k/signals.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.section .text

.global __mlibc_signal_restore
.type __mlibc_signal_restore, @function
__mlibc_signal_restore:
move.l (%sp)+, %d1
move.l #119, %d0
trap #0
illegal

.global __mlibc_signal_restore_rt
.type __mlibc_signal_restore_rt, @function
__mlibc_signal_restore_rt:
move.l (%sp)+, %d1
move.l #173, %d0
trap #0
illegal

.section .note.GNU-stack,"",%progbits
Loading

0 comments on commit dfc397a

Please sign in to comment.