xref: /freebsd/tools/test/stress2/misc/pmc8.sh (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1#!/bin/sh
2
3# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=227041
4
5# panic: [pmc,2749] (ri21, rc1) waiting too long for pmc to be free
6# cpuid = 23
7# time = 1624293771
8# KDB: stack backtrace:
9# db_trace_self_wrapper() at db_trace_self_wrapper+0x2b/frame 0xfffffe01ab083850
10# vpanic() at vpanic+0x181/frame 0xfffffe01ab0838a0
11# panic() at panic+0x43/frame 0xfffffe01ab083900
12# pmc_wait_for_pmc_idle() at pmc_wait_for_pmc_idle+0xa2/frame 0xfffffe01ab083930
13# pmc_release_pmc_descriptor() at pmc_release_pmc_descriptor+0x20b/frame 0xfffffe01ab083980
14# pmc_syscall_handler() at pmc_syscall_handler+0x4bd/frame 0xfffffe01ab083ac0
15# amd64_syscall() at amd64_syscall+0x762/frame 0xfffffe01ab083bf0
16# fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe01ab083bf0
17# --- syscall (0, FreeBSD ELF64, nosys), rip = 0x8009c154a, rsp = 0x7fffffffe4d8, rbp = 0x7fffffffe500 ---
18# KDB: enter: panic
19# [ thread pid 3123 tid 100454 ]
20# Stopped at      kdb_enter+0x37: movq    $0,0x1285d1e(%rip)
21# db> x/s version
22# version: FreeBSD 14.0-CURRENT #1 ufs-n247476-f77f86ecfea: Mon Jun 21 08:40:40 CEST 2021
23# pho@t2.osted.lan:/var/tmp/deviant3/sys/amd64/compile/PHO
24# db>
25
26. ../default.cfg
27cat > /tmp/pmc-crash.c <<EOF
28/*
29	Copyright (c) 2018 Dominic Dwyer <dom@itsallbroken.com>
30	All rights reserved.
31
32	Redistribution and use in source and binary forms, with or without
33	modification, are permitted provided that the following conditions are met:
34
35	1. Redistributions of source code must retain the above copyright notice,
36	   this list of conditions and the following disclaimer.
37
38	2. Redistributions in binary form must reproduce the above copyright notice,
39	   this list of conditions and the following disclaimer in the documentation
40	   and/or other materials provided with the distribution.
41
42	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
46	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52	POSSIBILITY OF SUCH DAMAGE.
53
54	clang -lpmc pmc-crash.c -o pmc-crash
55*/
56
57#include <sys/types.h>
58#include <errno.h>
59#include <pmc.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63#include <unistd.h>
64
65int main(void)
66{
67	if (pmc_init() != 0)
68	{
69		printf("pmc_init %s", strerror(errno));
70		exit(-1);
71	}
72
73	pmc_id_t pmc_id = 0;
74	if (pmc_allocate("inst_retired.any", PMC_MODE_TC, 0, PMC_CPU_ANY,
75	    &pmc_id, 1024) != 0)
76	{
77		printf("pmc_allocate %s", strerror(errno));
78		exit(-1);
79	}
80
81	printf("attaching to self\n");
82	if (pmc_attach(pmc_id, 0) != 0)
83	{
84		printf("pmc_attach %s", strerror(errno));
85		exit(-1);
86	}
87
88	if (pmc_start(pmc_id) != 0)
89	{
90		printf("pmc_start %s", strerror(errno));
91		exit(-1);
92	}
93
94	pmc_value_t v = 0;
95	for (int i = 0; i < 100; i++)
96	{
97		pmc_read(pmc_id, &v);
98	}
99	pmc_stop(pmc_id);
100
101	printf("success, detaching...\n");
102	pmc_detach(pmc_id, 0);
103	pmc_release(pmc_id);
104
105	printf("ok?");
106}
107EOF
108mycc -o /tmp/pmc-crash -Wall -Wextra -O0 /tmp/pmc-crash.c -lpmc || exit
109kldstat -v | grep -q hwpmc  || { kldload hwpmc; loaded=1; }
110here=`pwd`
111cd /tmp
112./pmc-crash; s=$?
113cd $here
114rm -f /tmp/pmc-crash /tmp/pmc-crash.core /tmp/pmc-crash.c
115[ $loaded ] && kldunload hwpmc
116exit $s
117