summaryrefslogtreecommitdiff
path: root/minix/servers/mib/hw.c
blob: 63de32c98623c153fd86bffb2e0067492d2588dd (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* MIB service - hw.c - implementation of the CTL_HW subtree */

#include "mib.h"

#if defined(__i386__)
static const char mach[] = "i386";	/* machine (cpu) type */
static const char arch[] = "i386";	/* architecture */
#elif defined(__arm__)
static const char mach[] = "evbarm";	/* machine (cpu) type */
static const char arch[] = "evbarm";	/* architecture */
#else
#error "unknown machine architecture"
#endif

/*
 * Implementation of CTL_HW HW_PHYSMEM/HW_PHYSMEM64.
 */
static ssize_t
mib_hw_physmem(struct mib_call * call __unused, struct mib_node * node,
	struct mib_oldp * oldp, struct mib_newp * newp __unused)
{
	struct vm_stats_info vsi;
	u_quad_t physmem64;
	unsigned int physmem;

	if (vm_info_stats(&vsi) != OK)
		return EINVAL;

	physmem64 = (u_quad_t)vsi.vsi_total * vsi.vsi_pagesize;

	if (node->node_size == sizeof(int)) {
		if (physmem64 > UINT_MAX)
			physmem = UINT_MAX;
		else
			physmem = (unsigned int)physmem64;

		return mib_copyout(oldp, 0, &physmem, sizeof(physmem));
	} else
		return mib_copyout(oldp, 0, &physmem64, sizeof(physmem64));
}

/*
 * Implementation of CTL_HW HW_USERMEM/HW_USERMEM64.
 */
static ssize_t
mib_hw_usermem(struct mib_call * call __unused, struct mib_node * node,
	struct mib_oldp * oldp, struct mib_newp * newp __unused)
{
	struct vm_stats_info vsi;
	struct vm_usage_info vui;
	u_quad_t usermem64;
	unsigned int usermem;

	if (vm_info_stats(&vsi) != OK)
		return EINVAL;

	usermem64 = (u_quad_t)vsi.vsi_total * vsi.vsi_pagesize;

	if (vm_info_usage(KERNEL, &vui) != OK)
		return EINVAL;

	if (usermem64 >= vui.vui_total)
		usermem64 -= vui.vui_total;
	else
		usermem64 = 0;

	if (node->node_size == sizeof(int)) {
		if (usermem64 > UINT_MAX)
			usermem = UINT_MAX;
		else
			usermem = (unsigned int)usermem64;

		return mib_copyout(oldp, 0, &usermem, sizeof(usermem));
	} else
		return mib_copyout(oldp, 0, &usermem64, sizeof(usermem64));
}

/*
 * Implementation of CTL_HW HW_NCPUONLINE.
 */
static ssize_t
mib_hw_ncpuonline(struct mib_call * call __unused,
	struct mib_node * node __unused, struct mib_oldp * oldp,
	struct mib_newp * newp __unused)
{
	struct machine machine;
	int ncpuonline;

	if (sys_getmachine(&machine) != OK)
		return EINVAL;

	ncpuonline = machine.processors_count;

	return mib_copyout(oldp, 0, &ncpuonline, sizeof(ncpuonline));
}

/* The CTL_HW nodes. */
static struct mib_node mib_hw_table[] = {
/* 1*/	[HW_MACHINE]		= MIB_STRING(_P | _RO, mach, "machine",
				    "Machine class"),
/* 2*/	/* HW_MODEL: not yet supported */
/* 3*/	[HW_NCPU]		= MIB_INT(_P | _RO, CONFIG_MAX_CPUS,
				    "ncpu", "Number of CPUs configured"),
/* 4*/	[HW_BYTEORDER]		= MIB_INT(_P | _RO, BYTE_ORDER, "byteorder",
				    "System byte order"),
/* 5*/	[HW_PHYSMEM]		= MIB_FUNC(_P | _RO | CTLFLAG_UNSIGNED |
				    CTLTYPE_INT, sizeof(int), mib_hw_physmem,
				    "physmem", "Bytes of physical memory"),
/* 6*/	[HW_USERMEM]		= MIB_FUNC(_P | _RO | CTLFLAG_UNSIGNED |
				    CTLTYPE_INT, sizeof(int), mib_hw_usermem,
				    "usermem", "Bytes of non-kernel memory"),
/* 7*/	[HW_PAGESIZE]		= MIB_INT(_P | _RO, PAGE_SIZE, "pagesize",
				    "Software page size"),
/* 8*/	/* HW_DISKNAMES: not yet supported */
/* 9*/	/* HW_IOSTATS: not yet supported */
/*10*/	[HW_MACHINE_ARCH]	= MIB_STRING(_P | _RO, arch, "machine_arch",
				    "Machine CPU class"),
/*11*/	/* HW_ALIGNBYTES: not yet supported */
/*12*/	/* HW_CNMAGIC: not yet supported */
/*13*/	[HW_PHYSMEM64]		= MIB_FUNC(_P | _RO | CTLTYPE_QUAD,
				    sizeof(u_quad_t), mib_hw_physmem,
				    "physmem64", "Bytes of physical memory"),
/*14*/	[HW_USERMEM64]		= MIB_FUNC(_P | _RO | CTLTYPE_QUAD,
				    sizeof(u_quad_t), mib_hw_usermem,
				    "usermem64", "Bytes of non-kernel memory"),
/*15*/	/* HW_IOSTATNAMES: not yet supported */
/*16*/	[HW_NCPUONLINE]		= MIB_FUNC(_P | _RO | CTLTYPE_INT, sizeof(int),
				    mib_hw_ncpuonline, "ncpuonline",
				    "Number of CPUs online"),
};

/*
 * Initialize the CTL_HW subtree.
 */
void
mib_hw_init(struct mib_node * node)
{

	MIB_INIT_ENODE(node, mib_hw_table);
}