1 /* MIB service - hw.c - implementation of the CTL_HW subtree */
2
3 #include "mib.h"
4
5 #if defined(__i386__)
6 static const char mach[] = "i386"; /* machine (cpu) type */
7 static const char arch[] = "i386"; /* architecture */
8 #elif defined(__arm__)
9 static const char mach[] = "evbarm"; /* machine (cpu) type */
10 static const char arch[] = "evbarm"; /* architecture */
11 #else
12 #error "unknown machine architecture"
13 #endif
14
15 /*
16 * Implementation of CTL_HW HW_PHYSMEM/HW_PHYSMEM64.
17 */
18 static ssize_t
mib_hw_physmem(struct mib_call * call __unused,struct mib_node * node,struct mib_oldp * oldp,struct mib_newp * newp __unused)19 mib_hw_physmem(struct mib_call * call __unused, struct mib_node * node,
20 struct mib_oldp * oldp, struct mib_newp * newp __unused)
21 {
22 struct vm_stats_info vsi;
23 u_quad_t physmem64;
24 unsigned int physmem;
25
26 if (vm_info_stats(&vsi) != OK)
27 return EINVAL;
28
29 physmem64 = (u_quad_t)vsi.vsi_total * vsi.vsi_pagesize;
30
31 if (node->node_size == sizeof(int)) {
32 if (physmem64 > UINT_MAX)
33 physmem = UINT_MAX;
34 else
35 physmem = (unsigned int)physmem64;
36
37 return mib_copyout(oldp, 0, &physmem, sizeof(physmem));
38 } else
39 return mib_copyout(oldp, 0, &physmem64, sizeof(physmem64));
40 }
41
42 /*
43 * Implementation of CTL_HW HW_USERMEM/HW_USERMEM64.
44 */
45 static ssize_t
mib_hw_usermem(struct mib_call * call __unused,struct mib_node * node,struct mib_oldp * oldp,struct mib_newp * newp __unused)46 mib_hw_usermem(struct mib_call * call __unused, struct mib_node * node,
47 struct mib_oldp * oldp, struct mib_newp * newp __unused)
48 {
49 struct vm_stats_info vsi;
50 struct vm_usage_info vui;
51 u_quad_t usermem64;
52 unsigned int usermem;
53
54 if (vm_info_stats(&vsi) != OK)
55 return EINVAL;
56
57 usermem64 = (u_quad_t)vsi.vsi_total * vsi.vsi_pagesize;
58
59 if (vm_info_usage(KERNEL, &vui) != OK)
60 return EINVAL;
61
62 if (usermem64 >= vui.vui_total)
63 usermem64 -= vui.vui_total;
64 else
65 usermem64 = 0;
66
67 if (node->node_size == sizeof(int)) {
68 if (usermem64 > UINT_MAX)
69 usermem = UINT_MAX;
70 else
71 usermem = (unsigned int)usermem64;
72
73 return mib_copyout(oldp, 0, &usermem, sizeof(usermem));
74 } else
75 return mib_copyout(oldp, 0, &usermem64, sizeof(usermem64));
76 }
77
78 /*
79 * Implementation of CTL_HW HW_NCPUONLINE.
80 */
81 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)82 mib_hw_ncpuonline(struct mib_call * call __unused,
83 struct mib_node * node __unused, struct mib_oldp * oldp,
84 struct mib_newp * newp __unused)
85 {
86 struct machine machine;
87 int ncpuonline;
88
89 if (sys_getmachine(&machine) != OK)
90 return EINVAL;
91
92 ncpuonline = machine.processors_count;
93
94 return mib_copyout(oldp, 0, &ncpuonline, sizeof(ncpuonline));
95 }
96
97 /* The CTL_HW nodes. */
98 static struct mib_node mib_hw_table[] = {
99 /* 1*/ [HW_MACHINE] = MIB_STRING(_P | _RO, mach, "machine",
100 "Machine class"),
101 /* 2*/ /* HW_MODEL: not yet supported */
102 /* 3*/ [HW_NCPU] = MIB_INT(_P | _RO, CONFIG_MAX_CPUS,
103 "ncpu", "Number of CPUs configured"),
104 /* 4*/ [HW_BYTEORDER] = MIB_INT(_P | _RO, BYTE_ORDER, "byteorder",
105 "System byte order"),
106 /* 5*/ [HW_PHYSMEM] = MIB_FUNC(_P | _RO | CTLFLAG_UNSIGNED |
107 CTLTYPE_INT, sizeof(int), mib_hw_physmem,
108 "physmem", "Bytes of physical memory"),
109 /* 6*/ [HW_USERMEM] = MIB_FUNC(_P | _RO | CTLFLAG_UNSIGNED |
110 CTLTYPE_INT, sizeof(int), mib_hw_usermem,
111 "usermem", "Bytes of non-kernel memory"),
112 /* 7*/ [HW_PAGESIZE] = MIB_INT(_P | _RO, PAGE_SIZE, "pagesize",
113 "Software page size"),
114 /* 8*/ /* HW_DISKNAMES: not yet supported */
115 /* 9*/ /* HW_IOSTATS: not yet supported */
116 /*10*/ [HW_MACHINE_ARCH] = MIB_STRING(_P | _RO, arch, "machine_arch",
117 "Machine CPU class"),
118 /*11*/ /* HW_ALIGNBYTES: not yet supported */
119 /*12*/ /* HW_CNMAGIC: not yet supported */
120 /*13*/ [HW_PHYSMEM64] = MIB_FUNC(_P | _RO | CTLTYPE_QUAD,
121 sizeof(u_quad_t), mib_hw_physmem,
122 "physmem64", "Bytes of physical memory"),
123 /*14*/ [HW_USERMEM64] = MIB_FUNC(_P | _RO | CTLTYPE_QUAD,
124 sizeof(u_quad_t), mib_hw_usermem,
125 "usermem64", "Bytes of non-kernel memory"),
126 /*15*/ /* HW_IOSTATNAMES: not yet supported */
127 /*16*/ [HW_NCPUONLINE] = MIB_FUNC(_P | _RO | CTLTYPE_INT, sizeof(int),
128 mib_hw_ncpuonline, "ncpuonline",
129 "Number of CPUs online"),
130 };
131
132 /*
133 * Initialize the CTL_HW subtree.
134 */
135 void
mib_hw_init(struct mib_node * node)136 mib_hw_init(struct mib_node * node)
137 {
138
139 MIB_INIT_ENODE(node, mib_hw_table);
140 }
141