18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: LGPL-2.1
28c2ecf20Sopenharmony_ci#include <sched.h>
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci/*
58c2ecf20Sopenharmony_ci * Not defined anywhere else, probably, just to make sure we
68c2ecf20Sopenharmony_ci * catch future flags
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#define SCHED_POLICY_MASK 0xff
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef SCHED_DEADLINE
118c2ecf20Sopenharmony_ci#define SCHED_DEADLINE 6
128c2ecf20Sopenharmony_ci#endif
138c2ecf20Sopenharmony_ci#ifndef SCHED_RESET_ON_FORK
148c2ecf20Sopenharmony_ci#define SCHED_RESET_ON_FORK 0x40000000
158c2ecf20Sopenharmony_ci#endif
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic size_t syscall_arg__scnprintf_sched_policy(char *bf, size_t size,
188c2ecf20Sopenharmony_ci						  struct syscall_arg *arg)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	bool show_prefix = arg->show_string_prefix;
218c2ecf20Sopenharmony_ci	const char *prefix = "SCHED_";
228c2ecf20Sopenharmony_ci	const char *policies[] = {
238c2ecf20Sopenharmony_ci		"NORMAL", "FIFO", "RR", "BATCH", "ISO", "IDLE", "DEADLINE",
248c2ecf20Sopenharmony_ci	};
258c2ecf20Sopenharmony_ci	size_t printed;
268c2ecf20Sopenharmony_ci	int policy = arg->val,
278c2ecf20Sopenharmony_ci	    flags = policy & ~SCHED_POLICY_MASK;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	policy &= SCHED_POLICY_MASK;
308c2ecf20Sopenharmony_ci	if (policy <= SCHED_DEADLINE)
318c2ecf20Sopenharmony_ci		printed = scnprintf(bf, size, "%s%s", show_prefix ? prefix : "", policies[policy]);
328c2ecf20Sopenharmony_ci	else
338c2ecf20Sopenharmony_ci		printed = scnprintf(bf, size, "%#x", policy);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define	P_POLICY_FLAG(n) \
368c2ecf20Sopenharmony_ci	if (flags & SCHED_##n) { \
378c2ecf20Sopenharmony_ci		printed += scnprintf(bf + printed, size - printed, "|%s%s", show_prefix ? prefix : "",  #n); \
388c2ecf20Sopenharmony_ci		flags &= ~SCHED_##n; \
398c2ecf20Sopenharmony_ci	}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	P_POLICY_FLAG(RESET_ON_FORK);
428c2ecf20Sopenharmony_ci#undef P_POLICY_FLAG
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	if (flags)
458c2ecf20Sopenharmony_ci		printed += scnprintf(bf + printed, size - printed, "|%#x", flags);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	return printed;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define SCA_SCHED_POLICY syscall_arg__scnprintf_sched_policy
51