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_POWERPC_R1];
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	stack->data = (char *) buf;
428c2ecf20Sopenharmony_ci	stack->size = stack_size;
438c2ecf20Sopenharmony_ci	return 0;
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciint test__arch_unwind_sample(struct perf_sample *sample,
478c2ecf20Sopenharmony_ci			     struct thread *thread)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	struct regs_dump *regs = &sample->user_regs;
508c2ecf20Sopenharmony_ci	u64 *buf;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	buf = calloc(1, sizeof(u64) * PERF_REGS_MAX);
538c2ecf20Sopenharmony_ci	if (!buf) {
548c2ecf20Sopenharmony_ci		pr_debug("failed to allocate sample uregs data\n");
558c2ecf20Sopenharmony_ci		return -1;
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	perf_regs_load(buf);
598c2ecf20Sopenharmony_ci	regs->abi  = PERF_SAMPLE_REGS_ABI;
608c2ecf20Sopenharmony_ci	regs->regs = buf;
618c2ecf20Sopenharmony_ci	regs->mask = PERF_REGS_MASK;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return sample_ustack(sample, thread, buf);
648c2ecf20Sopenharmony_ci}
65