[OpenAFS-devel] Re: Compiling openafs on arm64/aarch64 architecture

Chas Williams (CONTRACTOR) chas@cmf.nrl.navy.mil
Tue, 02 Sep 2014 20:06:43 -0400


In message <20140902161525.8c4f9270f04841ecb06700ca@sinenomine.net>,Andrew=
 Deason writes:
>It looks like glibc does have a getcontext/makecontext for aarch64, but
>I'm not looking into this deeply at the moment. I can verify later or
>someone else can speak up, but you can also just try it and see if it
>works :)

I have meant to fix this for a while (i.e. write a configure test).

Even though your glibc might have a ucontext that doesn't mean it works
(or works well enough).  For instance, I think MacOS' (ppc only perhaps)
and Linux/ppc32's implementation are sufficiently broken but present
in glibc.

Try building and running the following.  It should =


/*
 * this should print something like:

main: sp 0x7fff0c4ae318 alt_sp 0x136f010
main: swapcontext(&main_context, &thread_context)
thread: sp 0x1372fe0
thread: swapcontext(&thread_context, &main_context)
main: back from thread
 */

#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>

#define STACK_SIZE 16384

static ucontext_t main_context, thread_context;
static char *alt_stack;

static void
thread(void)
{
	unsigned long stack_ptr;
	unsigned long offset;

	printf("thread: sp %p\n", &stack_ptr);
	offset =3D (unsigned long) &stack_ptr - (unsigned long) alt_stack;
	if (offset > STACK_SIZE) {
		fprintf(stderr, "failed to switch to alternate stack?\n");
		exit(EXIT_FAILURE);
	}
	printf("thread: swapcontext(&thread_context, &main_context)\n");
	if (swapcontext(&thread_context, &main_context) =3D=3D -1)
		perror("swapcontext");
	exit(EXIT_FAILURE);
}

int
main(int argc, char *argv[])
{
	unsigned long stack_ptr;

	if (getcontext(&thread_context) =3D=3D -1) {
		perror("getcontext");
		exit(EXIT_FAILURE);
	}
	alt_stack =3D malloc(STACK_SIZE);
	if (!alt_stack) {
		perror("malloc");
		exit(EXIT_FAILURE);
	}
	printf("main: sp %p alt_sp %p\n", &stack_ptr, alt_stack);
	thread_context.uc_stack.ss_sp =3D alt_stack;
	thread_context.uc_stack.ss_size =3D STACK_SIZE;
	makecontext(&thread_context, thread, 0);

	printf("main: swapcontext(&main_context, &thread_context)\n");
	if (swapcontext(&main_context, &thread_context) =3D=3D -1) {
		perror("swapcontext");
		exit(EXIT_FAILURE);
	}

	printf("main: back from thread\n");
	free(alt_stack);
	exit(EXIT_SUCCESS);
}