18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <string.h>
38c2ecf20Sopenharmony_ci#include "perf_regs.h"
48c2ecf20Sopenharmony_ci#include "thread.h"
58c2ecf20Sopenharmony_ci#include "map.h"
68c2ecf20Sopenharmony_ci#include "maps.h"
78c2ecf20Sopenharmony_ci#include "event.h"
88c2ecf20Sopenharmony_ci#include "debug.h"
98c2ecf20Sopenharmony_ci#include "tests/tests.h"
108c2ecf20Sopenharmony_ci#include "arch-tests.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#define STACK_SIZE 8192
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic int sample_ustack(struct perf_sample *sample,
158c2ecf20Sopenharmony_ci			 struct thread *thread, u64 *regs)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	struct stack_dump *stack = &sample->user_stack;
188c2ecf20Sopenharmony_ci	struct map *map;
198c2ecf20Sopenharmony_ci	unsigned long sp;
208c2ecf20Sopenharmony_ci	u64 stack_size, *buf;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	buf = malloc(STACK_SIZE);
238c2ecf20Sopenharmony_ci	if (!buf) {
248c2ecf20Sopenharmony_ci		pr_debug("failed to allocate sample uregs data\n");
258c2ecf20Sopenharmony_ci		return -1;
268c2ecf20Sopenharmony_ci	}
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	sp = (unsigned long) regs[PERF_REG_X86_SP];
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	map = maps__find(thread->maps, (u64)sp);
318c2ecf20Sopenharmony_ci	if (!map) {
328c2ecf20Sopenharmony_ci		pr_debug("failed to get stack map\n");
338c2ecf20Sopenharmony_ci		free(buf);
348c2ecf20Sopenharmony_ci		return -1;
358c2ecf20Sopenharmony_ci	}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	stack_size = map->end - sp;
388c2ecf20Sopenharmony_ci	stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	memcpy(buf, (void *) sp, stack_size);
418c2ecf20Sopenharmony_ci#ifdef MEMORY_SANITIZER
428c2ecf20Sopenharmony_ci	/*
438c2ecf20Sopenharmony_ci	 * Copying the stack may copy msan poison, avoid false positives in the
448c2ecf20Sopenharmony_ci	 * unwinder by removing the poison here.
458c2ecf20Sopenharmony_ci	 */
468c2ecf20Sopenharmony_ci	__msan_unpoison(buf, stack_size);
478c2ecf20Sopenharmony_ci#endif
488c2ecf20Sopenharmony_ci	stack->data = (char *) buf;
498c2ecf20Sopenharmony_ci	stack->size = stack_size;
508c2ecf20Sopenharmony_ci	return 0;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciint test__arch_unwind_sample(struct perf_sample *sample,
548c2ecf20Sopenharmony_ci			     struct thread *thread)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct regs_dump *regs = &sample->user_regs;
578c2ecf20Sopenharmony_ci	u64 *buf;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	buf = malloc(sizeof(u64) * PERF_REGS_MAX);
608c2ecf20Sopenharmony_ci	if (!buf) {
618c2ecf20Sopenharmony_ci		pr_debug("failed to allocate sample uregs data\n");
628c2ecf20Sopenharmony_ci		return -1;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#ifdef MEMORY_SANITIZER
668c2ecf20Sopenharmony_ci	/*
678c2ecf20Sopenharmony_ci	 * Assignments to buf in the assembly function perf_regs_load aren't
688c2ecf20Sopenharmony_ci	 * seen by memory sanitizer. Zero the memory to convince memory
698c2ecf20Sopenharmony_ci	 * sanitizer the memory is initialized.
708c2ecf20Sopenharmony_ci	 */
718c2ecf20Sopenharmony_ci	memset(buf, 0, sizeof(u64) * PERF_REGS_MAX);
728c2ecf20Sopenharmony_ci#endif
738c2ecf20Sopenharmony_ci	perf_regs_load(buf);
748c2ecf20Sopenharmony_ci	regs->abi  = PERF_SAMPLE_REGS_ABI;
758c2ecf20Sopenharmony_ci	regs->regs = buf;
768c2ecf20Sopenharmony_ci	regs->mask = PERF_REGS_MASK;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	return sample_ustack(sample, thread, buf);
798c2ecf20Sopenharmony_ci}
80