aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/sysdeps/managarm/generic/sched.cpp
blob: bce8db860164977254856e5430c065e812b87fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/ensure.h>
#include <unistd.h>

#include <hel.h>
#include <hel-syscalls.h>
#include <mlibc/debug.hpp>
#include <mlibc/allocator.hpp>
#include <mlibc/posix-pipe.hpp>
#include <mlibc/posix-sysdeps.hpp>

#include <posix.frigg_bragi.hpp>

namespace mlibc {

int sys_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask) {
	return sys_getthreadaffinity(pid, cpusetsize, mask);
}

int sys_getthreadaffinity(pid_t tid, size_t cpusetsize, cpu_set_t *mask) {
	SignalGuard sguard;

	managarm::posix::GetAffinityRequest<MemoryAllocator> req(getSysdepsAllocator());

	req.set_pid(tid);
	req.set_size(cpusetsize);

	auto [offer, send_head, recv_resp, recv_data] =
		exchangeMsgsSync(
			getPosixLane(),
			helix_ng::offer(
				helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()),
				helix_ng::recvInline(),
				helix_ng::recvBuffer(mask, cpusetsize)
			)
		);

	HEL_CHECK(offer.error());
	HEL_CHECK(send_head.error());
	HEL_CHECK(recv_resp.error());

	managarm::posix::SvrResponse<MemoryAllocator> resp(getSysdepsAllocator());
	resp.ParseFromArray(recv_resp.data(), recv_resp.length());
	
	if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) {
		return EINVAL;
	} else if(resp.error() != managarm::posix::Errors::SUCCESS) {
		mlibc::infoLogger() << "mlibc: got unexpected error from posix in sys_getaffinity!" << frg::endlog;
		return EIEIO;
	}
	HEL_CHECK(recv_data.error());

	return 0;
}

int sys_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask) {
	return sys_setthreadaffinity(pid, cpusetsize, mask);
}

int sys_setthreadaffinity(pid_t tid, size_t cpusetsize, const cpu_set_t *mask) {
	SignalGuard sguard;

	frg::vector<uint8_t, MemoryAllocator> affinity_mask(getSysdepsAllocator());
	affinity_mask.resize(cpusetsize);
	memcpy(affinity_mask.data(), mask, cpusetsize);
	managarm::posix::SetAffinityRequest<MemoryAllocator> req(getSysdepsAllocator());

	req.set_pid(tid);
	req.set_mask(affinity_mask);

	auto [offer, send_head, send_tail, recv_resp] =
		exchangeMsgsSync(
			getPosixLane(),
			helix_ng::offer(
				helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()),
				helix_ng::recvInline()
			)
		);

	HEL_CHECK(offer.error());
	HEL_CHECK(send_head.error());
	HEL_CHECK(send_tail.error());
	HEL_CHECK(recv_resp.error());

	managarm::posix::SvrResponse<MemoryAllocator> resp(getSysdepsAllocator());
	resp.ParseFromArray(recv_resp.data(), recv_resp.length());
	
	if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) {
		return EINVAL;
	} else if(resp.error() != managarm::posix::Errors::SUCCESS) {
		mlibc::infoLogger() << "mlibc: got unexpected error from posix in sys_getaffinity!" << frg::endlog;
		return EIEIO;
	}

	return 0;
}

int sys_getcpu(int *cpu) {
	HEL_CHECK(helGetCurrentCpu(cpu));
	return 0;
}

}