Skip to content

Commit

Permalink
Merge pull request #1149 from netbsduser/m68k-port-new
Browse files Browse the repository at this point in the history
  • Loading branch information
64 authored Oct 13, 2024
2 parents 3b0bc20 + 02fffc1 commit d1ba689
Show file tree
Hide file tree
Showing 38 changed files with 1,267 additions and 11 deletions.
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
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ if default_library_paths.length() == 0
'riscv64': 64,
'riscv32': 32,
'aarch64': 64,
'arm': 32
'arm': 32,
'm68k': 32,
}
if target_word_size.get(target_machine.cpu_family()) == 64
default_library_paths = ['/lib', '/lib64', '/usr/lib', '/usr/lib64']
Expand Down
24 changes: 24 additions & 0 deletions options/ansi/include/bits/ansi/fenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@
#define FE_DOWNWARD 2
#define FE_UPWARD 3

#elif defined (__m68k__)

#if __HAVE_68881__ || __mcffpu__ || __HAVE_FPU_

#define FE_INEXACT 8
#define FE_DIVBYZERO 16
#define FE_UNDERFLOW 32
#define FE_OVERFLOW 64
#define FE_INVALID 128

#define FE_ALL_EXCEPT 0xf8

#define FE_TONEAREST 0
#define FE_TOWARDZERO 16
#define FE_DOWNWARD 32
#define FE_UPWARD 48

#else

#define FE_ALL_EXCEPT 0
#define FE_TONEAREST 0

#endif

#else
#error Unknown architecture
#endif
Expand Down
14 changes: 14 additions & 0 deletions options/elf/include/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,20 @@ enum {
R_RISCV_IRELATIVE = 58
};

enum {
R_68K_NONE = 0,
R_68K_32 = 1,
R_68K_PC32 = 4,
R_68K_COPY = 19,
R_68K_GLOB_DAT = 20,
R_68K_JMP_SLOT = 21,
R_68K_RELATIVE = 22,

R_68K_TLS_DTPMOD32 = 40,
R_68K_TLS_DTPREL32= 41,
R_68K_TLS_TPREL32= 42,
};

typedef struct {
Elf32_Addr r_offset;
Elf32_Word r_info;
Expand Down
2 changes: 1 addition & 1 deletion options/elf/include/link.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern "C" {
#if defined(__x86_64__) || defined(__aarch64__) \
|| (defined(__riscv) && __riscv_xlen == 64)
# define ElfW(type) Elf64_ ## type
#elif defined(__i386__)
#elif defined(__i386__) || defined(__m68k__)
# define ElfW(type) Elf32_ ## type
#else
# error Unknown architecture
Expand Down
17 changes: 17 additions & 0 deletions options/internal/include/bits/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ struct __mlibc_jmpbuf_register_state {
double fs10;
double fs11;
};
#elif defined (__m68k__)
struct __mlibc_jmpbuf_register_state {
uint32_t d2;
uint32_t d3;
uint32_t d4;
uint32_t d5;
uint32_t d6;
uint32_t d7;
uint32_t a2;
uint32_t a3;
uint32_t a4;
uint32_t a5;
uint32_t a6;
uint32_t a7;
uint32_t sp;
uint32_t pc;
};
#else
# error "Missing architecture specific code"
#endif
Expand Down
44 changes: 44 additions & 0 deletions options/internal/include/bits/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ typedef __mlibc_int64 __mlibc_int_fast64;
#define __MLIBC_INT_FAST64_MAX __MLIBC_INT64_MAX
#define __MLIBC_INT_FAST64_MIN __MLIBC_INT64_MIN

#elif defined (__m68k__)

typedef __mlibc_int8 __mlibc_int_fast8;
#define __MLIBC_INT_FAST8_C(x) __MLIBC_INT8_C(x)
#define __MLIBC_INT_FAST8_MAX __MLIBC_INT8_MAX
#define __MLIBC_INT_FAST8_MIN __MLIBC_INT8_MIN

typedef __mlibc_int32 __mlibc_int_fast16;
#define __MLIBC_INT_FAST16_C(x) __MLIBC_INT16_C(x)
#define __MLIBC_INT_FAST16_MAX __MLIBC_INT16_MAX
#define __MLIBC_INT_FAST16_MIN __MLIBC_INT16_MIN

typedef __mlibc_int32 __mlibc_int_fast32;
#define __MLIBC_INT_FAST32_C(x) __MLIBC_INT32_C(x)
#define __MLIBC_INT_FAST32_MAX __MLIBC_INT32_MAX
#define __MLIBC_INT_FAST32_MIN __MLIBC_INT32_MIN

typedef __mlibc_int64 __mlibc_int_fast64;
#define __MLIBC_INT_FAST64_C(x) __MLIBC_INT64_C(x)
#define __MLIBC_INT_FAST64_MAX __MLIBC_INT64_MAX
#define __MLIBC_INT_FAST64_MIN __MLIBC_INT64_MIN

#else
# error "Missing architecture specific code"
#endif
Expand Down Expand Up @@ -242,6 +264,28 @@ typedef __mlibc_uint64 __mlibc_uint_fast64;
#define __MLIBC_UINT_FAST64_MAX __MLIBC_UINT64_MAX
#define __MLIBC_UINT_FAST64_MIN __MLIBC_UINT64_MIN

#elif defined (__m68k__)

typedef __mlibc_uint8 __mlibc_uint_fast8;
#define __MLIBC_UINT_FAST8_C(x) __MLIBC_UINT8_C(x)
#define __MLIBC_UINT_FAST8_MAX __MLIBC_UINT8_MAX
#define __MLIBC_UINT_FAST8_MIN __MLIBC_UINT8_MIN

typedef __mlibc_uint32 __mlibc_uint_fast16;
#define __MLIBC_UINT_FAST16_C(x) __MLIBC_UINT16_C(x)
#define __MLIBC_UINT_FAST16_MAX __MLIBC_UINT16_MAX
#define __MLIBC_UINT_FAST16_MIN __MLIBC_UINT16_MIN

typedef __mlibc_uint32 __mlibc_uint_fast32;
#define __MLIBC_UINT_FAST32_C(x) __MLIBC_UINT32_C(x)
#define __MLIBC_UINT_FAST32_MAX __MLIBC_UINT32_MAX
#define __MLIBC_UINT_FAST32_MIN __MLIBC_UINT32_MIN

typedef __mlibc_uint64 __mlibc_uint_fast64;
#define __MLIBC_UINT_FAST64_C(x) __MLIBC_UINT64_C(x)
#define __MLIBC_UINT_FAST64_MAX __MLIBC_UINT64_MAX
#define __MLIBC_UINT_FAST64_MIN __MLIBC_UINT64_MIN

#else
# error "Missing architecture specific code"
#endif
Expand Down
5 changes: 5 additions & 0 deletions options/internal/include/mlibc/tcb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ static_assert(sizeof(Tcb) - offsetof(Tcb, cancelBits) - TP_TCB_OFFSET == 80);
// sysdeps/linux/riscv64/cp_syscall.S needs to be updated whenever
// the struct is expanded.
static_assert(sizeof(Tcb) - offsetof(Tcb, cancelBits) == 96);
#elif defined (__m68k__)
// The thread pointer on m68k points to 0x7000 bytes *after* the end of the
// TCB, so similarly to as on RISC-V, we need to keep the value in
// sysdeps/linux/m68k/cp_syscall.S up-to-date.
static_assert(sizeof(Tcb) - offsetof(Tcb, cancelBits) == 0x30);
#else
#error "Missing architecture specific code."
#endif
13 changes: 13 additions & 0 deletions options/internal/m68k-include/mlibc/arch-defs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef MLIBC_ARCH_DEFS_HPP
#define MLIBC_ARCH_DEFS_HPP

#include <stddef.h>

namespace mlibc {

// not strictly true, can be 4 or 8k on 68040/68060, and many more on others
inline constexpr size_t page_size = 0x1000;

} // namespace mlibc

#endif // MLIBC_ARCH_DEFS_HPP
24 changes: 24 additions & 0 deletions options/internal/m68k-include/mlibc/thread.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <stdint.h>
#include <mlibc/internal-sysdeps.hpp>
#include <mlibc/tcb.hpp>
#include <bits/ensure.h>

namespace mlibc {

extern "C" void *__m68k_read_tp();

inline Tcb *get_current_tcb() {
// On m68k, the end of the TCB is 0x7000 below the thread pointer.
void *ptr = __m68k_read_tp();
return reinterpret_cast<Tcb *>((uintptr_t)ptr - 0x7000 - sizeof(Tcb));
}

inline uintptr_t get_sp() {
uintptr_t sp;
asm ("move.l %%sp, %0" : "=r"(sp));
return sp;
}

} // namespace mlibc
Loading

0 comments on commit d1ba689

Please sign in to comment.