vm: fix potential null deref
[minix.git] / servers / procfs / root.c
blobf97a9dad47afbc09a8a93b81df81fe66a9e4351b
1 /* ProcFS - root.c - by Alen Stojanov and David van Moolenbroek */
3 #include "inc.h"
4 #include <machine/pci.h>
5 #include <minix/dmap.h>
6 #include "cpuinfo.h"
8 static void root_hz(void);
9 static void root_uptime(void);
10 static void root_loadavg(void);
11 static void root_kinfo(void);
12 static void root_meminfo(void);
13 static void root_pci(void);
14 static void root_dmap(void);
15 static void root_ipcvecs(void);
17 struct file root_files[] = {
18 { "hz", REG_ALL_MODE, (data_t) root_hz },
19 { "uptime", REG_ALL_MODE, (data_t) root_uptime },
20 { "loadavg", REG_ALL_MODE, (data_t) root_loadavg },
21 { "kinfo", REG_ALL_MODE, (data_t) root_kinfo },
22 { "meminfo", REG_ALL_MODE, (data_t) root_meminfo },
23 { "pci", REG_ALL_MODE, (data_t) root_pci },
24 { "dmap", REG_ALL_MODE, (data_t) root_dmap },
25 { "cpuinfo", REG_ALL_MODE, (data_t) root_cpuinfo },
26 { "ipcvecs", REG_ALL_MODE, (data_t) root_ipcvecs },
27 { NULL, 0, NULL }
30 /*===========================================================================*
31 * root_hz *
32 *===========================================================================*/
33 static void root_hz(void)
35 /* Print the system clock frequency.
38 buf_printf("%lu\n", (long) sys_hz());
41 /*===========================================================================*
42 * root_loadavg *
43 *===========================================================================*/
44 static void root_loadavg(void)
46 /* Print load averages.
48 struct load loads[3];
49 ldiv_t avg[3];
51 if (procfs_getloadavg(loads, 3) != 3)
52 return;
54 avg[0] = ldiv(100L * loads[0].proc_load / loads[0].ticks, 100);
55 avg[1] = ldiv(100L * loads[1].proc_load / loads[1].ticks, 100);
56 avg[2] = ldiv(100L * loads[2].proc_load / loads[2].ticks, 100);
58 buf_printf("%ld.%0.2ld %ld.%02ld %ld.%02ld\n",
59 avg[0].quot, avg[0].rem, avg[1].quot, avg[1].rem,
60 avg[2].quot, avg[2].rem);
63 /*===========================================================================*
64 * root_uptime *
65 *===========================================================================*/
66 static void root_uptime(void)
68 /* Print the current uptime.
70 clock_t ticks;
71 ldiv_t division;
73 if (getuptime(&ticks) != OK)
74 return;
75 division = ldiv(100L * ticks / sys_hz(), 100L);
77 buf_printf("%ld.%0.2ld\n", division.quot, division.rem);
80 /*===========================================================================*
81 * root_kinfo *
82 *===========================================================================*/
83 static void root_kinfo(void)
85 /* Print general kernel information.
87 struct kinfo kinfo;
89 if (sys_getkinfo(&kinfo) != OK)
90 return;
92 buf_printf("%u %u\n", kinfo.nr_procs, kinfo.nr_tasks);
95 /*===========================================================================*
96 * root_meminfo *
97 *===========================================================================*/
98 static void root_meminfo(void)
100 /* Print general memory information.
102 struct vm_stats_info vsi;
104 if (vm_info_stats(&vsi) != OK)
105 return;
107 buf_printf("%u %lu %lu %lu %lu\n", vsi.vsi_pagesize,
108 vsi.vsi_total, vsi.vsi_free, vsi.vsi_largest, vsi.vsi_cached);
111 /*===========================================================================*
112 * root_pci *
113 *===========================================================================*/
114 static void root_pci(void)
116 /* Print information about PCI devices present in the system.
118 u16_t vid, did;
119 u8_t bcr, scr, pifr;
120 char *slot_name, *dev_name;
121 int r, devind;
122 static int first = TRUE;
124 /* This should be taken care of behind the scenes by the PCI lib. */
125 if (first) {
126 pci_init();
127 first = FALSE;
130 /* Iterate over all devices, printing info for each of them. */
131 r = pci_first_dev(&devind, &vid, &did);
132 while (r == 1) {
133 slot_name = pci_slot_name(devind);
134 dev_name = pci_dev_name(vid, did);
136 bcr = pci_attr_r8(devind, PCI_BCR);
137 scr = pci_attr_r8(devind, PCI_SCR);
138 pifr = pci_attr_r8(devind, PCI_PIFR);
140 buf_printf("%s %x/%x/%x %04X:%04X %s\n",
141 slot_name ? slot_name : "-",
142 bcr, scr, pifr, vid, did,
143 dev_name ? dev_name : "");
145 r = pci_next_dev(&devind, &vid, &did);
149 /*===========================================================================*
150 * root_dmap *
151 *===========================================================================*/
152 static void root_dmap(void)
154 struct dmap dmap[NR_DEVICES];
155 int i;
157 if (getsysinfo(VFS_PROC_NR, SI_DMAP_TAB, dmap, sizeof(dmap)) != OK)
158 return;
160 for (i = 0; i < NR_DEVICES; i++) {
161 if (dmap[i].dmap_driver == NONE)
162 continue;
164 buf_printf("%u %s %u\n", i, dmap[i].dmap_label,
165 dmap[i].dmap_driver);
169 /*===========================================================================*
170 * root_ipcvecs *
171 *===========================================================================*/
172 static void root_ipcvecs(void)
174 extern struct minix_kerninfo *_minix_kerninfo;
175 extern struct minix_ipcvecs _minix_ipcvecs;
177 /* only print this if the kernel provides the info; otherwise binaries
178 * will be using their own in-libc vectors that are normal symbols in the
179 * binary.
181 if(!_minix_kerninfo || !(_minix_kerninfo->ki_flags & MINIX_KIF_IPCVECS))
182 return;
184 /* print the vectors with an descriptive name and the additional (k)
185 * to distinguish them from regular symbols.
187 #define PRINT_ENTRYPOINT(name) \
188 buf_printf("%08lx T %s(k)\n", _minix_ipcvecs.name ## _ptr, #name)
190 PRINT_ENTRYPOINT(sendrec);
191 PRINT_ENTRYPOINT(send);
192 PRINT_ENTRYPOINT(notify);
193 PRINT_ENTRYPOINT(senda);
194 PRINT_ENTRYPOINT(sendnb);
195 PRINT_ENTRYPOINT(receive);
196 PRINT_ENTRYPOINT(do_kernel_call);