Skip to content

Commit

Permalink
Add LoongArch64 framework code support
Browse files Browse the repository at this point in the history
Mainly added some environment configurations, macro definitions, specific
architecture structures and some function declarations supported by the
LoongArch64 architecture.

Co-developed-by: Youling Tang <[email protected]>
Signed-off-by: Youling Tang <[email protected]>
Signed-off-by: Ming Wang <[email protected]>
  • Loading branch information
Ming Wang authored and k-hagio committed Jan 26, 2024
1 parent 28891d1 commit 35a2472
Show file tree
Hide file tree
Showing 11 changed files with 13,162 additions and 10 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ CFILES=main.c tools.c global_data.c memory.c filesys.c help.c task.c \
kernel.c test.c gdb_interface.c configure.c net.c dev.c bpf.c \
printk.c \
alpha.c x86.c ppc.c ia64.c s390.c s390x.c s390dbf.c ppc64.c x86_64.c \
arm.c arm64.c mips.c mips64.c riscv64.c sparc64.c \
arm.c arm64.c mips.c mips64.c riscv64.c loongarch64.c sparc64.c \
extensions.c remote.c va_server.c va_server_v1.c symbols.c cmdline.c \
lkcd_common.c lkcd_v1.c lkcd_v2_v3.c lkcd_v5.c lkcd_v7.c lkcd_v8.c\
lkcd_fix_mem.c s390_dump.c lkcd_x86_trace.c \
Expand All @@ -85,7 +85,7 @@ OBJECT_FILES=main.o tools.o global_data.o memory.o filesys.o help.o task.o \
build_data.o kernel.o test.o gdb_interface.o net.o dev.o bpf.o \
printk.o \
alpha.o x86.o ppc.o ia64.o s390.o s390x.o s390dbf.o ppc64.o x86_64.o \
arm.o arm64.o mips.o mips64.o riscv64.o sparc64.o \
arm.o arm64.o mips.o mips64.o riscv64.o loongarch64.o sparc64.o \
extensions.o remote.o va_server.o va_server_v1.o symbols.o cmdline.o \
lkcd_common.o lkcd_v1.o lkcd_v2_v3.o lkcd_v5.o lkcd_v7.o lkcd_v8.o \
lkcd_fix_mem.o s390_dump.o netdump.o diskdump.o makedumpfile.o xendump.o \
Expand Down Expand Up @@ -445,6 +445,9 @@ riscv64.o: ${GENERIC_HFILES} ${REDHAT_HFILES} riscv64.c
sparc64.o: ${GENERIC_HFILES} ${REDHAT_HFILES} sparc64.c
${CC} -c ${CRASH_CFLAGS} sparc64.c ${WARNING_OPTIONS} ${WARNING_ERROR}

loongarch64.o: ${GENERIC_HFILES} ${REDHAT_HFILES} loongarch64.c
${CC} -c ${CRASH_CFLAGS} loongarch64.c ${WARNING_OPTIONS} ${WARNING_ERROR}

s390.o: ${GENERIC_HFILES} ${IBM_HFILES} s390.c
${CC} -c ${CRASH_CFLAGS} s390.c ${WARNING_OPTIONS} ${WARNING_ERROR}

Expand Down
43 changes: 42 additions & 1 deletion configure.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ void add_extra_lib(char *);
#undef SPARC64
#undef MIPS64
#undef RISCV64
#undef LOONGARCH64

#define UNKNOWN 0
#define X86 1
Expand All @@ -124,6 +125,7 @@ void add_extra_lib(char *);
#define SPARC64 12
#define MIPS64 13
#define RISCV64 14
#define LOONGARCH64 15

#define TARGET_X86 "TARGET=X86"
#define TARGET_ALPHA "TARGET=ALPHA"
Expand All @@ -139,6 +141,7 @@ void add_extra_lib(char *);
#define TARGET_MIPS64 "TARGET=MIPS64"
#define TARGET_SPARC64 "TARGET=SPARC64"
#define TARGET_RISCV64 "TARGET=RISCV64"
#define TARGET_LOONGARCH64 "TARGET=LOONGARCH64"

#define TARGET_CFLAGS_X86 "TARGET_CFLAGS=-D_FILE_OFFSET_BITS=64"
#define TARGET_CFLAGS_ALPHA "TARGET_CFLAGS="
Expand All @@ -163,6 +166,8 @@ void add_extra_lib(char *);
#define TARGET_CFLAGS_SPARC64 "TARGET_CFLAGS="
#define TARGET_CFLAGS_RISCV64 "TARGET_CFLAGS="
#define TARGET_CFLAGS_RISCV64_ON_X86_64 "TARGET_CFLAGS="
#define TARGET_CFLAGS_LOONGARCH64 "TARGET_CFLAGS="
#define TARGET_CFLAGS_LOONGARCH64_ON_X86_64 "TARGET_CFLAGS="

#define GDB_TARGET_DEFAULT "GDB_CONF_FLAGS="
#define GDB_TARGET_ARM_ON_X86 "GDB_CONF_FLAGS=--target=arm-elf-linux"
Expand All @@ -174,6 +179,7 @@ void add_extra_lib(char *);
#define GDB_TARGET_MIPS_ON_X86 "GDB_CONF_FLAGS=--target=mipsel-elf-linux"
#define GDB_TARGET_MIPS_ON_X86_64 "GDB_CONF_FLAGS=--target=mipsel-elf-linux CFLAGS=-m32 CXXFLAGS=-m32"
#define GDB_TARGET_RISCV64_ON_X86_64 "GDB_CONF_FLAGS=--target=riscv64-unknown-linux-gnu"
#define GDB_TARGET_LOONGARCH64_ON_X86_64 "GDB_CONF_FLAGS=--target=loongarch64-unknown-linux-gnu"

/*
* The original plan was to allow the use of a particular version
Expand Down Expand Up @@ -413,6 +419,9 @@ get_current_configuration(struct supported_gdb_version *sp)
#if defined(__riscv) && (__riscv_xlen == 64)
target_data.target = RISCV64;
#endif
#ifdef __loongarch64
target_data.target = LOONGARCH64;
#endif

set_initial_target(sp);

Expand Down Expand Up @@ -472,6 +481,12 @@ get_current_configuration(struct supported_gdb_version *sp)
* Build an RISCV64 crash binary on an X86_64 host.
*/
target_data.target = RISCV64;
} else if ((target_data.target == X86_64) &&
(name_to_target((char *)target_data.target_as_param) == LOONGARCH64)) {
/*
* Build an LOONGARCH64 crash binary on an X86_64 host.
*/
target_data.target = LOONGARCH64;
} else {
fprintf(stderr,
"\ntarget=%s is not supported on the %s host architecture\n\n",
Expand Down Expand Up @@ -512,6 +527,14 @@ get_current_configuration(struct supported_gdb_version *sp)
(target_data.target != MIPS64))
arch_mismatch(sp);

if ((target_data.initial_gdb_target == LOONGARCH64) &&
(target_data.target != LOONGARCH64)) {
if (target_data.target == X86_64)
target_data.target = LOONGARCH64;
else
arch_mismatch(sp);
}

if ((target_data.initial_gdb_target == RISCV64) &&
(target_data.target != RISCV64)) {
if (target_data.target == X86_64)
Expand Down Expand Up @@ -686,6 +709,9 @@ show_configuration(void)
case RISCV64:
printf("TARGET: RISCV64\n");
break;
case LOONGARCH64:
printf("TARGET: LOONGARCH64\n");
break;
}

if (strlen(target_data.program)) {
Expand Down Expand Up @@ -811,6 +837,14 @@ build_configure(struct supported_gdb_version *sp)
} else
target_CFLAGS = TARGET_CFLAGS_RISCV64;
break;
case LOONGARCH64:
target = TARGET_LOONGARCH64;
if (target_data.host == X86_64) {
target_CFLAGS = TARGET_CFLAGS_LOONGARCH64_ON_X86_64;
gdb_conf_flags = GDB_TARGET_LOONGARCH64_ON_X86_64;
} else
target_CFLAGS = TARGET_CFLAGS_LOONGARCH64;
break;
}

ldflags = get_extra_flags("LDFLAGS.extra", NULL);
Expand Down Expand Up @@ -1408,7 +1442,7 @@ make_spec_file(struct supported_gdb_version *sp)
printf("Vendor: Red Hat, Inc.\n");
printf("Packager: Dave Anderson <[email protected]>\n");
printf("ExclusiveOS: Linux\n");
printf("ExclusiveArch: %%{ix86} alpha ia64 ppc ppc64 ppc64pseries ppc64iseries x86_64 s390 s390x arm aarch64 ppc64le mips mipsel mips64el sparc64 riscv64\n");
printf("ExclusiveArch: %%{ix86} alpha ia64 ppc ppc64 ppc64pseries ppc64iseries x86_64 s390 s390x arm aarch64 ppc64le mips mipsel mips64el sparc64 riscv64 loongarch64\n");
printf("Buildroot: %%{_tmppath}/%%{name}-root\n");
printf("BuildRequires: ncurses-devel zlib-devel bison\n");
printf("Requires: binutils\n");
Expand Down Expand Up @@ -1649,6 +1683,8 @@ set_initial_target(struct supported_gdb_version *sp)
target_data.initial_gdb_target = SPARC64;
else if (strncmp(buf, "RISCV64", strlen("RISCV64")) == 0)
target_data.initial_gdb_target = RISCV64;
else if (strncmp(buf, "LOONGARCH64", strlen("LOONGARCH64")) == 0)
target_data.initial_gdb_target = LOONGARCH64;
}

char *
Expand All @@ -1670,6 +1706,7 @@ target_to_name(int target)
case MIPS64: return("MIPS64");
case SPARC64: return("SPARC64");
case RISCV64: return("RISCV64");
case LOONGARCH64: return("LOONGARCH64");
}

return "UNKNOWN";
Expand Down Expand Up @@ -1738,6 +1775,10 @@ name_to_target(char *name)
return RISCV64;
else if (strncmp(name, "riscv64", strlen("riscv64")) == 0)
return RISCV64;
else if (strncmp(name, "loongarch64", strlen("loongarch64")) == 0)
return LOONGARCH64;
else if (strncmp(name, "LOONGARCH64", strlen("LOONGARCH64")) == 0)
return LOONGARCH64;

return UNKNOWN;
}
Expand Down
162 changes: 161 additions & 1 deletion defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#if !defined(X86) && !defined(X86_64) && !defined(ALPHA) && !defined(PPC) && \
!defined(IA64) && !defined(PPC64) && !defined(S390) && !defined(S390X) && \
!defined(ARM) && !defined(ARM64) && !defined(MIPS) && !defined(MIPS64) && \
!defined(RISCV64) && !defined(SPARC64)
!defined(RISCV64) && !defined(LOONGARCH64) && !defined(SPARC64)
#ifdef __alpha__
#define ALPHA
#endif
Expand Down Expand Up @@ -121,6 +121,9 @@
#if defined(__riscv) && (__riscv_xlen == 64)
#define RISCV64
#endif
#ifdef __loongarch64
#define LOONGARCH64
#endif
#endif

#ifdef X86
Expand Down Expand Up @@ -165,6 +168,9 @@
#ifdef RISCV64
#define NR_CPUS (256)
#endif
#ifdef LOONGARCH64
#define NR_CPUS (256)
#endif

#define NR_DEVICE_DUMPS (64)

Expand Down Expand Up @@ -3723,6 +3729,48 @@ typedef signed int s32;

#endif /* RISCV64 */

/* fix compilation errors due to elf.h version. */
#ifndef EM_LOONGARCH
#define EM_LOONGARCH 258
#endif

#ifdef LOONGARCH64
#define _64BIT_
#define MACHINE_TYPE "LOONGARCH64"

#define PAGEBASE(X) (((ulong)(X)) & (ulong)machdep->pagemask)

#define IS_XKPRANGE(X) (((X) >= 0x8000000000000000lu) && \
((X) < 0xc000000000000000lu))

#define PTOV(X) ((ulong)(X) + 0x9000000000000000lu)
#define VTOP(X) ((ulong)(X) & 0x0000fffffffffffflu)

#define IS_VMALLOC_ADDR(X) (vt->vmalloc_start && (ulong)(X) >= vt->vmalloc_start)

#define DEFAULT_MODULES_VADDR 0xffff800000000000lu
#define MODULES_VADDR (machdep->machspec->modules_vaddr)
#define MODULES_END (machdep->machspec->modules_end)
#define VMALLOC_START (machdep->machspec->vmalloc_start_addr)
#define VMALLOC_END (machdep->machspec->vmalloc_end)

#define __SWP_TYPE_SHIFT 16
#define __SWP_TYPE_BITS 8
#define __SWP_TYPE_MASK ((1 << __SWP_TYPE_BITS) - 1)
#define __SWP_OFFSET_SHIFT (__SWP_TYPE_BITS + __SWP_TYPE_SHIFT)

#define SWP_TYPE(entry) (((entry) >> __SWP_TYPE_SHIFT) & __SWP_TYPE_MASK)
#define SWP_OFFSET(entry) ((entry) >> __SWP_OFFSET_SHIFT)

#define __swp_type(entry) SWP_TYPE(entry)
#define __swp_offset(entry) SWP_OFFSET(entry)

#define TIF_SIGPENDING (1)

#define _SECTION_SIZE_BITS 28
#define _MAX_PHYSMEM_BITS 48
#endif /* LOONGARCH64 */

#ifdef X86
#define _32BIT_
#define MACHINE_TYPE "X86"
Expand Down Expand Up @@ -4775,6 +4823,10 @@ struct machine_specific {
#define MAX_HEXADDR_STRLEN (16)
#define UVADDR_PRLEN (16)
#endif
#ifdef LOONGARCH64
#define MAX_HEXADDR_STRLEN (16)
#define UVADDR_PRLEN (16)
#endif

#define BADADDR ((ulong)(-1))
#define BADVAL ((ulong)(-1))
Expand Down Expand Up @@ -5396,6 +5448,9 @@ void dump_build_data(void);
#ifdef SPARC64
#define machdep_init(X) sparc64_init(X)
#endif
#ifdef LOONGARCH64
#define machdep_init(X) loongarch64_init(X)
#endif
int clean_exit(int);
int untrusted_file(FILE *, char *);
char *readmem_function_name(void);
Expand Down Expand Up @@ -5887,6 +5942,10 @@ void display_help_screen(char *);
#ifdef RISCV64
#define dump_machdep_table(X) riscv64_dump_machdep_table(X)
#endif
#ifdef LOONGARCH64
#define dump_machdep_table(X) loongarch64_dump_machdep_table(X)
#endif

extern char *help_pointer[];
extern char *help_alias[];
extern char *help_ascii[];
Expand Down Expand Up @@ -7109,6 +7168,107 @@ int sparc64_vmalloc_addr(ulong);
error(FATAL, "The -d option is not applicable to sparc64.\n")
#endif

/*
* loongarch64.c
*/
void loongarch64_display_regs_from_elf_notes(int, FILE *);
#ifdef LOONGARCH64
void loongarch64_init(int);
void loongarch64_dump_machdep_table(ulong);

#define display_idt_table() \
error(FATAL, "-d option is not applicable to LOONGARCH64 architecture\n")


#define KSYMS_START (0x1)

struct machine_specific {
ulong phys_base;
ulong vmalloc_start_addr;
ulong modules_vaddr;
ulong modules_end;

struct loongarch64_pt_regs *crash_task_regs;
};

/*
* Basic page table format:
*
* 63 62 61 PALEN-1 12 10 9 8 7 6 5 4 3 2 1 0
* +----+--+--+------+--------------------+----+--+--+-+-+-+---+---+-+-+
* |RPLV|NX|NR| | PA[PALEN-1:12] | |SP|PN|W|P|G|MAT|PLV|D|V|
* +----+--+--+------+--------------------+----+--+--+-+-+-+---+---+-+-+
*
*
* Huge page table format:
*
* 63 62 61 PALEN-1 12 10 9 8 7 6 5 4 3 2 1 0
* +----+--+--+------+-----------------+--+----+--+--+-+-+-+---+---+-+-+
* |RPLV|NX|NR| | PA[PALEN-1:12] | G| |SP|PN|W|P|H|MAT|PLV|D|V|
* +----+--+--+------+-----------------+--+----+--+--+-+-+-+---+---+-+-+
*
*/
/* from arch/loongarch/include/asm/pgtable-bits.h */

/* Page table bits */
#define _PAGE_VALID_SHIFT 0
#define _PAGE_ACCESSED_SHIFT 0 /* Reuse Valid for Accessed */
#define _PAGE_DIRTY_SHIFT 1
#define _PAGE_PLV_SHIFT 2 /* 2~3, two bits */
#define _CACHE_SHIFT 4 /* 4~5, two bits */
#define _PAGE_GLOBAL_SHIFT 6
#define _PAGE_HUGE_SHIFT 6 /* HUGE is a PMD bit */
#define _PAGE_PRESENT_SHIFT 7
#define _PAGE_WRITE_SHIFT 8
#define _PAGE_MODIFIED_SHIFT 9
#define _PAGE_PROTNONE_SHIFT 10
#define _PAGE_SPECIAL_SHIFT 11
#define _PAGE_HGLOBAL_SHIFT 12 /* HGlobal is a PMD bit */
#define _PAGE_PFN_SHIFT 12
#define _PAGE_SWP_EXCLUSIVE_SHIFT 23
#define _PAGE_PFN_END_SHIFT 48
#define _PAGE_PRESENT_INVALID_SHIFT 60
#define _PAGE_NO_READ_SHIFT 61
#define _PAGE_NO_EXEC_SHIFT 62
#define _PAGE_RPLV_SHIFT 63

#ifndef _ULCAST_
#define _ULCAST_ (unsigned long)
#endif

/* Used by software */
#define _PAGE_PRESENT (_ULCAST_(1) << _PAGE_PRESENT_SHIFT)
#define _PAGE_PRESENT_INVALID (_ULCAST_(1) << _PAGE_PRESENT_INVALID_SHIFT)
#define _PAGE_WRITE (_ULCAST_(1) << _PAGE_WRITE_SHIFT)
#define _PAGE_ACCESSED (_ULCAST_(1) << _PAGE_ACCESSED_SHIFT)
#define _PAGE_MODIFIED (_ULCAST_(1) << _PAGE_MODIFIED_SHIFT)
#define _PAGE_PROTNONE (_ULCAST_(1) << _PAGE_PROTNONE_SHIFT)
#define _PAGE_SPECIAL (_ULCAST_(1) << _PAGE_SPECIAL_SHIFT)

/* We borrow bit 23 to store the exclusive marker in swap PTEs. */
#define _PAGE_SWP_EXCLUSIVE (_ULCAST_(1) << _PAGE_SWP_EXCLUSIVE_SHIFT)

/* Used by TLB hardware (placed in EntryLo*) */
#define _PAGE_VALID (_ULCAST_(1) << _PAGE_VALID_SHIFT)
#define _PAGE_DIRTY (_ULCAST_(1) << _PAGE_DIRTY_SHIFT)
#define _PAGE_PLV (_ULCAST_(3) << _PAGE_PLV_SHIFT)
#define _PAGE_GLOBAL (_ULCAST_(1) << _PAGE_GLOBAL_SHIFT)
#define _PAGE_HUGE (_ULCAST_(1) << _PAGE_HUGE_SHIFT)
#define _PAGE_HGLOBAL (_ULCAST_(1) << _PAGE_HGLOBAL_SHIFT)
#define _PAGE_NO_READ (_ULCAST_(1) << _PAGE_NO_READ_SHIFT)
#define _PAGE_NO_EXEC (_ULCAST_(1) << _PAGE_NO_EXEC_SHIFT)
#define _PAGE_RPLV (_ULCAST_(1) << _PAGE_RPLV_SHIFT)
#define _CACHE_MASK (_ULCAST_(3) << _CACHE_SHIFT)
#define _PFN_SHIFT (PAGESHIFT() - 12 + _PAGE_PFN_SHIFT)

#define _PAGE_USER (PLV_USER << _PAGE_PLV_SHIFT)
#define _PAGE_KERN (PLV_KERN << _PAGE_PLV_SHIFT)

#define _PFN_MASK (~((_ULCAST_(1) << (_PFN_SHIFT)) - 1) & \
((_ULCAST_(1) << (_PAGE_PFN_END_SHIFT)) - 1))

#endif /* LOONGARCH64 */

/*
* netdump.c
*/
Expand Down
Loading

0 comments on commit 35a2472

Please sign in to comment.