Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i#5383 mac a64, part 5: Get hello,world to run #6184

Merged
merged 4 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions api/samples/stl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ DR_EXPORT void
dr_init(client_id_t client_id)
{
int i;
#ifdef SHOW_RESULTS
bool success = true;
#endif

dr_set_client_name("DynamoRIO Sample Client 'stl_test'",
"http://dynamorio.org/issues");
Expand Down Expand Up @@ -109,10 +111,10 @@ dr_init(client_id_t client_id)
for (i = 0; i < 5; i++) {
#ifdef SHOW_RESULTS
std::cout << (*v)[i];
#endif
if ((*v)[i] != i) {
success = false;
}
#endif
}
delete v;

Expand All @@ -132,10 +134,10 @@ dr_init(client_id_t client_id)
for (std::list<int>::iterator l_iter = l.begin(); l_iter != l.end(); l_iter++) {
#ifdef SHOW_RESULTS
std::cout << *l_iter;
#endif
if (*l_iter != i) {
success = false;
}
#endif
i++;
}

Expand All @@ -154,10 +156,10 @@ dr_init(client_id_t client_id)
for (i = 0; i < 5; i++) {
#ifdef SHOW_RESULTS
std::cout << m[i];
#endif
if (m[i] != i) {
success = false;
}
#endif
}

//
Expand Down
13 changes: 12 additions & 1 deletion clients/drcachesim/tests/allasm_aarch64_flush.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2020 Google, Inc. All rights reserved.
* Copyright (c) 2020-2023 Google, Inc. All rights reserved.
* Copyright (c) 2016 ARM Limited. All rights reserved.
* **********************************************************/

Expand Down Expand Up @@ -42,7 +42,13 @@ _start:
sub x0, x0, #1024 // x0 is bottom of region
mov sp, x0

#ifdef __APPLE__
// XXX: Try to include asm_defines.asm for AARCH64_ADR_GOT().
adrp x0, helloworld@PAGE
add x0, x0, helloworld@PAGEOFF
#else
adr x0, helloworld
#endif
adr x1, .

// Data cache flush operations.
Expand All @@ -55,7 +61,12 @@ _start:

// Exit.
mov w0, #1 // stdout
#ifdef __APPLE__
adrp x1, helloworld@PAGE
add x1, x1, helloworld@PAGEOFF
#else
adr x1, helloworld
#endif
mov w2, #14 // sizeof(helloworld)
mov w8, #64 // SYS_write
svc #0
Expand Down
13 changes: 11 additions & 2 deletions clients/drcachesim/tests/allasm_aarch64_prefetch.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2020 Google, Inc. All rights reserved.
* Copyright (c) 2020-2023 Google, Inc. All rights reserved.
* Copyright (c) 2016 ARM Limited. All rights reserved.
* **********************************************************/

Expand Down Expand Up @@ -42,8 +42,12 @@ _start:
sub x0, x0, #1024 // x0 is bottom of region
mov sp, x0


#ifdef __APPLE__
adrp x0, helloworld@PAGE
add x0, x0, helloworld@PAGEOFF
#else
adr x0, helloworld
#endif
adr x1, .

// prefetch_read_l1
Expand Down Expand Up @@ -165,7 +169,12 @@ _start:

// Exit.
mov w0, #1 // stdout
#ifdef __APPLE__
adrp x1, helloworld@PAGE
add x1, x1, helloworld@PAGEOFF
#else
adr x1, helloworld
#endif
mov w2, #14 // sizeof(helloworld)
mov w8, #64 // SYS_write
svc #0
Expand Down
19 changes: 12 additions & 7 deletions core/arch/aarch64/aarch64.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2019-2022 Google, Inc. All rights reserved.
* Copyright (c) 2019-2023 Google, Inc. All rights reserved.
* Copyright (c) 2016 ARM Limited. All rights reserved.
* **********************************************************/

Expand Down Expand Up @@ -105,6 +105,12 @@ DECL_EXTERN(dr_setjmp_sigmask)

DECL_EXTERN(d_r_internal_error)

DECL_EXTERN(exiting_thread_count)
DECL_EXTERN(d_r_initstack)
DECL_EXTERN(initstack_mutex)
DECL_EXTERN(icache_op_struct)
DECL_EXTERN(linkstub_selfmod)

/* For debugging: report an error if the function called by call_switch_stack()
* unexpectedly returns. Also used elsewhere.
*/
Expand Down Expand Up @@ -325,7 +331,7 @@ GLOBAL_LABEL(cleanup_and_terminate:)
#endif

/* inc exiting_thread_count to avoid being killed once off all_threads list */
AARCH64_ADRP_GOT_LDR(GLOBAL_REF(exiting_thread_count), x0)
AARCH64_ADRP_GOT(GLOBAL_REF(exiting_thread_count), x0)
CALLC2(GLOBAL_REF(atomic_add), x0, #1)

/* save dcontext->dstack for freeing later and set dcontext->is_exiting */
Expand All @@ -349,7 +355,7 @@ cat_thread_only:
CALLC0(GLOBAL_REF(dynamo_thread_exit))
cat_no_thread:
/* switch to d_r_initstack for cleanup of dstack */
AARCH64_ADRP_GOT_LDR(GLOBAL_REF(initstack_mutex), x26)
AARCH64_ADRP_GOT(GLOBAL_REF(initstack_mutex), x26)
cat_spin:
CALLC2(GLOBAL_REF(atomic_swap), x26, #1)
cbz w0, cat_have_lock
Expand All @@ -358,20 +364,20 @@ cat_spin:

cat_have_lock:
/* switch stack */
AARCH64_ADRP_GOT_LDR(GLOBAL_REF(d_r_initstack), x0)
AARCH64_ADRP_GOT(GLOBAL_REF(d_r_initstack), x0)
ldr x0, [x0]
mov sp, x0

/* free dstack and call the EXIT_DR_HOOK */
CALLC1(GLOBAL_REF(dynamo_thread_stack_free_and_exit), x24) /* pass dstack */

/* give up initstack_mutex */
AARCH64_ADRP_GOT_LDR(GLOBAL_REF(initstack_mutex), x0)
AARCH64_ADRP_GOT(GLOBAL_REF(initstack_mutex), x0)
mov x1, #0
str x1, [x0]

/* dec exiting_thread_count (allows another thread to kill us) */
AARCH64_ADRP_GOT_LDR(GLOBAL_REF(exiting_thread_count), x0)
AARCH64_ADRP_GOT(GLOBAL_REF(exiting_thread_count), x0)
CALLC2(GLOBAL_REF(atomic_add), x0, #-1)

/* put system call number in x8 */
Expand Down Expand Up @@ -401,7 +407,6 @@ GLOBAL_LABEL(atomic_add:)
DECLARE_FUNC(global_do_syscall_int)
GLOBAL_LABEL(global_do_syscall_int:)
#ifdef MACOS
mov x16, #0
svc #0x80
#else
/* FIXME i#1569: NYI on AArch64 */
Expand Down
5 changes: 1 addition & 4 deletions core/arch/aarchxx/emit_utils.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2014-2022 Google, Inc. All rights reserved.
* Copyright (c) 2014-2023 Google, Inc. All rights reserved.
* Copyright (c) 2016 ARM Limited. All rights reserved.
* **********************************************************/

Expand Down Expand Up @@ -66,9 +66,6 @@ insert_load_dr_tls_base(dcontext_t *dcontext, instrlist_t *ilist, instr_t *where
/* ldr dr_reg_stolen, [reg_base, DR_TLS_BASE_OFFSET] */
PRE(ilist, where,
XINST_CREATE_load(dcontext, opnd_create_reg(dr_reg_stolen),
/* TODO i#5383: For MACOS && AARCH64 we need to verify the
* offset: PR #5497 had an 8x multiplier here.
*/
OPND_CREATE_MEMPTR(reg_base, DR_TLS_BASE_OFFSET)));
}

Expand Down
20 changes: 13 additions & 7 deletions core/arch/asm_defines.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2011-2022 Google, Inc. All rights reserved.
* Copyright (c) 2011-2023 Google, Inc. All rights reserved.
* Copyright (c) 2008-2009 VMware, Inc. All rights reserved.
* ********************************************************** */

Expand Down Expand Up @@ -106,20 +106,26 @@

# if defined(MACOS) && defined(AARCH64)

# define DECLARE_FUNC(symbol) \
/* The Mac assembler isn't resolving macro args so we have to force it to
* support FUNCNAME used in many files.
*/
# define DECLARE_FUNC(symbol) DECLARE_FUNC_EVAL(symbol)
# define DECLARE_FUNC_EVAL(symbol) \
.p2align 2 @N@ \
.globl _##symbol @N@ \
.private_extern _##symbol @N@ \
.private_extern _##symbol @N@

# define DECLARE_EXPORTED_FUNC(symbol) \
# define DECLARE_EXPORTED_FUNC(symbol) DECLARE_EXPORTED_FUNC_EVAL(symbol)
# define DECLARE_EXPORTED_FUNC_EVAL(symbol) \
.p2align 2 @N@ \
.globl _##symbol @N@ \
.globl _##symbol @N@

# define DECLARE_GLOBAL(symbol) \
.globl _##symbol @N@\
.private_extern _##symbol

# define GLOBAL_LABEL(label) _##label
# define GLOBAL_LABEL(label) GLOBAL_LABEL_EVAL(label)
# define GLOBAL_LABEL_EVAL(label) _##label
# define GLOBAL_REF(label) _##label

# define AARCH64_ADRP_GOT(sym, reg) \
Expand All @@ -128,7 +134,7 @@ add reg, reg, sym@PAGEOFF

# define AARCH64_ADRP_GOT_LDR(sym, reg) \
adrp reg, sym@PAGE @N@ \
add reg, reg, sym@PAGEOFF
ldr reg, [reg, sym@PAGEOFF]

# else

Expand Down
17 changes: 1 addition & 16 deletions core/ir/aarch64/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -5878,21 +5878,6 @@ extract_tsz_offset(uint enc, uint tszh_pos, uint tszl_pos)
return offset;
}

static inline aarch64_reg_offset
extract_tsz3_offset(uint enc, uint tszh_pos, uint tszl_pos)
{
int offset;

ASSERT(tszh_pos < 30);
uint tsz = (extract_uint(enc, tszh_pos, 1) << 2) | extract_uint(enc, tszl_pos, 2);

if (!highest_bit_set(tsz, 0, 3, &offset))
return NOT_A_REG;

ASSERT(offset < 4);
return offset;
}

static inline bool
decode_opnd_z_wtszl19p1_bhsd_5(uint enc, int opcode, byte *pc, OUT opnd_t *opnd)
{
Expand Down Expand Up @@ -9183,7 +9168,7 @@ decode_common(dcontext_t *dcontext, byte *pc, byte *orig_pc, instr_t *instr)
*/
if ((enc & 0xfffff01f) == 0xd503201f) {
SYSLOG_INTERNAL_WARNING("Undefined HINT instruction found: "
"encoding 0x%x (CRm:op2 0x%x)\n",
"encoding 0x%x (CRm:op2 0x%x)",
enc, (enc & 0xfe0) >> 5);
instr_set_opcode(instr, OP_nop);
instr_set_num_opnds(dcontext, instr, 0, 0);
Expand Down
20 changes: 15 additions & 5 deletions core/unix/os.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* *******************************************************************************
* Copyright (c) 2010-2022 Google, Inc. All rights reserved.
* Copyright (c) 2010-2023 Google, Inc. All rights reserved.
* Copyright (c) 2011 Massachusetts Institute of Technology All rights reserved.
* Copyright (c) 2000-2010 VMware, Inc. All rights reserved.
* *******************************************************************************/
Expand Down Expand Up @@ -1329,14 +1329,22 @@ uint
query_time_seconds(void)
{
struct timeval current_time;
#if defined(MACOS) && defined(AARCH64)
/* TODO i#5383: Replace with a system call (unless we're sure this library call
* is re-entrant, just a simple load from commpage).
*/
# undef gettimeofday /* Remove "gettimeofday_forbidden_function". */
uint64 val = gettimeofday(&current_time, NULL);
#else
uint64 val = dynamorio_syscall(SYS_gettimeofday, 2, &current_time, NULL);
#ifdef MACOS
# ifdef MACOS
/* MacOS before Sierra returns usecs:secs and does not set the timeval struct. */
if (macos_version < MACOS_VERSION_SIERRA) {
if ((int)val < 0)
return 0;
return (uint)val + UTC_TO_EPOCH_SECONDS;
}
# endif
#endif
if ((int)val >= 0) {
return current_time.tv_sec + UTC_TO_EPOCH_SECONDS;
Expand All @@ -1358,7 +1366,9 @@ query_time_millis()
#if !(defined(MACOS) && defined(AARCH64))
uint64 val = dynamorio_syscall(SYS_gettimeofday, 2, &current_time, NULL);
#else
/* TODO i#5383: Replace with a system call. */
/* TODO i#5383: Replace with a system call (unless we're sure this library call
* is re-entrant, just a simple load from commpage).
*/
# undef gettimeofday /* Remove "gettimeofday_forbidden_function". */
uint64 val = gettimeofday(&current_time, NULL);
#endif
Expand Down Expand Up @@ -2103,7 +2113,7 @@ get_segment_base(uint seg)
{
#if defined(MACOS64) && defined(X86)
ptr_uint_t *pthread_self = (ptr_uint_t *)read_thread_register(seg);
return (byte *)&pthread_self[SEG_TLS_BASE_OFFSET];
return (byte *)&pthread_self[SEG_TLS_BASE_SLOT];
#elif defined(X86)
if (seg == SEG_CS || seg == SEG_SS || seg == SEG_DS || seg == SEG_ES)
return NULL;
Expand Down Expand Up @@ -2158,7 +2168,7 @@ get_local_state()
void
os_enter_dynamorio(void)
{
# if defined(AARCHXX) || defined(RISCV64)
# if (defined(AARCHXX) || defined(RISCV64)) && !defined(MACOS)
/* i#1578: check that app's tls value doesn't match our sentinel */
ASSERT(*(byte **)get_dr_tls_base_addr() != TLS_SLOT_VAL_EXITED);
# endif
Expand Down
7 changes: 4 additions & 3 deletions core/unix/os_exports.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@
* limited interoperability w/ code targeting the Windows x64 ABI. We steal slot 6
* for our own use.
*/
# define SEG_TLS_BASE_OFFSET 28 /* offset from pthread_t struct to segment base */
# define DR_TLS_BASE_SLOT 6 /* the TLS slot for DR's TLS base */
/* XXX i#5383: This is used as *8 so it's really a slot not a byte offset. */
# define SEG_TLS_BASE_SLOT 28 /* offset from pthread_t struct to segment base */
# define DR_TLS_BASE_SLOT 6 /* the TLS slot for DR's TLS base */
/* offset from pthread_t struct to slot 6 */
# define DR_TLS_BASE_OFFSET (SEG_TLS_BASE_OFFSET + DR_TLS_BASE_SLOT)
# define DR_TLS_BASE_OFFSET (sizeof(void *) * (SEG_TLS_BASE_SLOT + DR_TLS_BASE_SLOT))
#endif

#if defined(AARCHXX) && !defined(MACOS64)
Expand Down
11 changes: 8 additions & 3 deletions core/unix/tls_macos.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* *******************************************************************************
* Copyright (c) 2013-2022 Google, Inc. All rights reserved.
* Copyright (c) 2013-2023 Google, Inc. All rights reserved.
* *******************************************************************************/

/*
Expand Down Expand Up @@ -185,7 +185,7 @@ tls_get_dr_addr(void)
byte **
get_app_tls_swap_slot_addr(void)
{
byte **app_tls_base = (byte **)read_thread_register(TLS_REG_LIB);
byte *app_tls_base = (byte *)read_thread_register(TLS_REG_LIB);
if (app_tls_base == NULL) {
ASSERT_NOT_IMPLEMENTED(false);
}
Expand All @@ -199,7 +199,12 @@ tls_thread_init(os_local_state_t *os_tls, byte *segment)
#ifdef X64
/* For now we have both a directly-addressable os_local_state_t and a pointer to
* it in slot 6. If we settle on always doing the full os_local_state_t in slots,
* we would probably get rid of the use of slot 6.
* we would probably get rid of the use of slot 6 on x86 (on aarch64 the
* os_local_state_t slots are not directly addressible; we rely on the stolen
* register, whose value is populated from the pointer in slot 6 -- which could
* be moved to a slot right before os_local_state_t or something I suppose, or
* we could move the whole os_local_state_t to our own mmap since we access
* through a pointer anyway).
*/
byte **tls_swap_slot;
ASSERT((byte *)(os_tls->self) == segment);
Expand Down
Loading