From 71ec2dd172f2e97a272f30515dc373002e82a50a Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sun, 11 Nov 2018 00:59:44 +0100
Subject: [PATCH 1/2] kernel: Fix build with -p.

---
 sys/cpu/x86_64/include/profile.h      |  2 +-
 sys/platform/pc64/apic/apic_vector.s  |  4 +-
 sys/platform/pc64/conf/files          |  1 +
 sys/platform/pc64/isa/prof_machdep.c  | 80 ++++++++++++++++-----------
 sys/platform/pc64/x86_64/exception.S  |  5 +-
 sys/platform/pc64/x86_64/mp_machdep.c |  3 +
 sys/platform/pc64/x86_64/msi_vector.s |  4 +-
 7 files changed, 58 insertions(+), 41 deletions(-)

diff --git a/sys/cpu/x86_64/include/profile.h b/sys/cpu/x86_64/include/profile.h
index d2be59e7a5..5906deabcb 100644
--- a/sys/cpu/x86_64/include/profile.h
+++ b/sys/cpu/x86_64/include/profile.h
@@ -60,7 +60,7 @@
 #else
 #define	MCOUNT_DECL(s)	u_long s;
 extern int	mcount_lock;
-#define	MCOUNT_ENTER(s)	{ s = read_rflags(); disable_intr(); \
+#define	MCOUNT_ENTER(s)	{ s = read_rflags(); cpu_disable_intr(); \
  			  while (!atomic_cmpset_acq_int(&mcount_lock, 0, 1)) \
 			  	/* nothing */ ; }
 #define	MCOUNT_EXIT(s)	{ atomic_store_rel_int(&mcount_lock, 0); \
diff --git a/sys/platform/pc64/apic/apic_vector.s b/sys/platform/pc64/apic/apic_vector.s
index 5372432bcd..3342128f9a 100644
--- a/sys/platform/pc64/apic/apic_vector.s
+++ b/sys/platform/pc64/apic/apic_vector.s
@@ -403,7 +403,7 @@ Xtimer:
 	MEXITCOUNT
 	APIC_POP_FRAME(jmp doreti_iret)
 
-MCOUNT_LABEL(bintr)
+MCOUNT_LABEL(bintr_apic)
 	INTR_HANDLER(0)
 	INTR_HANDLER(1)
 	INTR_HANDLER(2)
@@ -596,7 +596,7 @@ MCOUNT_LABEL(bintr)
 	INTR_HANDLER(189)
 	INTR_HANDLER(190)
 	INTR_HANDLER(191)
-MCOUNT_LABEL(eintr)
+MCOUNT_LABEL(eintr_apic)
 
 	.data
 
diff --git a/sys/platform/pc64/conf/files b/sys/platform/pc64/conf/files
index a44de54244..802b8dd9b7 100644
--- a/sys/platform/pc64/conf/files
+++ b/sys/platform/pc64/conf/files
@@ -216,6 +216,7 @@ dev/misc/syscons/syscons_nexus.c	optional	sc
 bus/isa/vga_isa.c			optional	vga
 platform/pc64/isa/clock.c		standard
 platform/pc64/isa/isa_intr.c		optional	isa
+platform/pc64/isa/prof_machdep.c	optional	profiling-routine
 platform/pc64/x86_64/spinlock.s		standard
 
 vmx_genassym.o				standard					\
diff --git a/sys/platform/pc64/isa/prof_machdep.c b/sys/platform/pc64/isa/prof_machdep.c
index aad2509330..07cd0639af 100644
--- a/sys/platform/pc64/isa/prof_machdep.c
+++ b/sys/platform/pc64/isa/prof_machdep.c
@@ -41,7 +41,7 @@
 #undef MCOUNT
 #endif
 
-#include <machine/asmacros.h>
+#include <machine/asm.h>
 
 #include <bus/isa/isa.h>
 #include <machine_base/isa/timerreg.h>
@@ -59,6 +59,9 @@ static int	cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
 #endif /* GUPROF */
 
 #ifdef __GNUC__
+#define	MPLOCK
+#define	MPUNLOCK
+
 __asm("								\n\
 GM_STATE	=	0					\n\
 GMON_PROF_OFF	=	3					\n\
@@ -71,52 +74,63 @@ __mcount:							\n\
 	#							\n\
 	# Check that we are profiling.  Do it early for speed.	\n\
 	#							\n\
-	cmpl	$GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
- 	je	.mcount_exit					\n\
- 	#							\n\
- 	# __mcount is the same as [.]mcount except the caller	\n\
- 	# hasn't changed the stack except to call here, so the	\n\
+	cmpl	$GMON_PROF_OFF,_gmonparam+GM_STATE		\n\
+	je	.mcount_exit					\n\
+	#							\n\
+	# __mcount is the same as [.]mcount except the caller	\n\
+	# hasn't changed the stack except to call here, so the	\n\
 	# caller's raddr is above our raddr.			\n\
 	#							\n\
- 	pushl	%eax						\n\
- 	pushl	%ecx						\n\
- 	pushl	%edx						\n\
- 	movl	12+4(%esp),%edx					\n\
- 	jmp	.got_frompc					\n\
- 								\n\
- 	.p2align 4,0x90						\n\
- 	.globl	" __XSTRING(HIDENAME(mcount)) "			\n\
-" __XSTRING(HIDENAME(mcount)) ":				\n\
-	cmpl	$GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
+	pushq	%rax						\n\
+	pushq	%rdx						\n\
+	pushq	%rcx						\n\
+	pushq	%rsi						\n\
+	pushq	%rdi						\n\
+	pushq	%r8						\n\
+	pushq	%r9						\n\
+	movq	7*8+8(%rsp),%rdi				\n\
+	jmp	.got_frompc					\n\
+								\n\
+	.p2align 4,0x90						\n\
+	.globl	.mcount						\n\
+.mcount:							\n\
+	cmpl	$GMON_PROF_OFF,_gmonparam+GM_STATE		\n\
 	je	.mcount_exit					\n\
 	#							\n\
 	# The caller's stack frame has already been built, so	\n\
-	# %ebp is the caller's frame pointer.  The caller's	\n\
+	# %rbp is the caller's frame pointer.  The caller's	\n\
 	# raddr is in the caller's frame following the caller's	\n\
 	# caller's frame pointer.				\n\
 	#							\n\
- 	pushl	%eax						\n\
- 	pushl	%ecx						\n\
- 	pushl	%edx						\n\
-	movl	4(%ebp),%edx					\n\
+	pushq	%rax						\n\
+	pushq	%rdx						\n\
+	pushq	%rcx						\n\
+	pushq	%rsi						\n\
+	pushq	%rdi						\n\
+	pushq	%r8						\n\
+	pushq	%r9						\n\
+	movq	8(%rbp),%rdi					\n\
 .got_frompc:							\n\
 	#							\n\
 	# Our raddr is the caller's pc.				\n\
 	#							\n\
-	movl	(%esp),%eax					\n\
+	movq	7*8(%rsp),%rsi					\n\
 								\n\
-	pushfl							\n\
-	pushl	%eax						\n\
-	pushl	%edx						\n\
-	cli							\n\
-	call	" __XSTRING(CNAME(mcount)) "			\n\
-	addl	$8,%esp						\n\
-	popfl							\n\
- 	popl	%edx						\n\
- 	popl	%ecx						\n\
- 	popl	%eax						\n\
+	pushfq							\n\
+	cli							\n"
+	MPLOCK "						\n\
+	call	mcount						\n"
+	MPUNLOCK "						\n\
+	popfq							\n\
+	popq	%r9						\n\
+	popq	%r8						\n\
+	popq	%rdi						\n\
+	popq	%rsi						\n\
+	popq	%rcx						\n\
+	popq	%rdx						\n\
+	popq	%rax						\n\
 .mcount_exit:							\n\
-	ret							\n\
+	ret	$0						\n\
 ");
 #else /* !__GNUC__ */
 #error
diff --git a/sys/platform/pc64/x86_64/exception.S b/sys/platform/pc64/x86_64/exception.S
index 395c8f8060..65a694f618 100644
--- a/sys/platform/pc64/x86_64/exception.S
+++ b/sys/platform/pc64/x86_64/exception.S
@@ -77,7 +77,6 @@
  */
 
 MCOUNT_LABEL(user)
-MCOUNT_LABEL(btrap)
 
 /*
  * Interrupts must be disabled for all traps, otherwise horrible %gs
@@ -694,7 +693,7 @@ ENTRY(fork_trampoline)
 
 	.text
 	SUPERALIGN_TEXT
-MCOUNT_LABEL(bintr)
+MCOUNT_LABEL(btrap)
 
 #if 0 /* JG */
 #include <x86_64/x86_64/apic_vector.S>
@@ -710,4 +709,4 @@ MCOUNT_LABEL(bintr)
 #endif
 
 	.text
-MCOUNT_LABEL(eintr)
+MCOUNT_LABEL(etrap)
diff --git a/sys/platform/pc64/x86_64/mp_machdep.c b/sys/platform/pc64/x86_64/mp_machdep.c
index 1382b5ab47..7fe2a17bb4 100644
--- a/sys/platform/pc64/x86_64/mp_machdep.c
+++ b/sys/platform/pc64/x86_64/mp_machdep.c
@@ -144,6 +144,9 @@ extern int naps;
 int64_t tsc0_offset;
 extern int64_t tsc_offsets[];
 
+/* lock region used by kernel profiling */
+int	mcount_lock;
+
 /* AP uses this during bootstrap.  Do not staticize.  */
 char *bootSTK;
 static int bootAP;
diff --git a/sys/platform/pc64/x86_64/msi_vector.s b/sys/platform/pc64/x86_64/msi_vector.s
index b2e8f9e14f..db64c41577 100644
--- a/sys/platform/pc64/x86_64/msi_vector.s
+++ b/sys/platform/pc64/x86_64/msi_vector.s
@@ -83,7 +83,7 @@ IDTVEC(msi_intr##irq_num) ;						\
 	jmp	doreti ;						\
 
 
-MCOUNT_LABEL(bintr)
+MCOUNT_LABEL(bintr_msi)
 	MSI_HANDLER(0)
 	MSI_HANDLER(1)
 	MSI_HANDLER(2)
@@ -276,7 +276,7 @@ MCOUNT_LABEL(bintr)
 	MSI_HANDLER(189)
 	MSI_HANDLER(190)
 	MSI_HANDLER(191)
-MCOUNT_LABEL(eintr)
+MCOUNT_LABEL(eintr_msi)
 
 	.data
 
-- 
2.19.1

