devarch.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include "ureg.h"
  16. typedef struct Cpuflag {
  17. const char *name; /* short name (linux-like)*/
  18. uint32_t eax; /* input eax */
  19. uint8_t infoidx; /* index of info result */
  20. uint8_t bitidx; /* feature bit in info result */
  21. } Cpuflag;
  22. // Below infoidxs equate to: 0=eax 1=ebx 2=ecx 3=edx
  23. Cpuflag cpuflags[] = {
  24. /* name eax info bit */
  25. { "fpu", 0x00000001, 3, 0, },
  26. { "vme", 0x00000001, 3, 1, },
  27. { "de", 0x00000001, 3, 2, },
  28. { "pse", 0x00000001, 3, 3, },
  29. { "tsc", 0x00000001, 3, 4, },
  30. { "msr", 0x00000001, 3, 5, },
  31. { "pae", 0x00000001, 3, 6, },
  32. { "mce", 0x00000001, 3, 7, },
  33. { "cx8", 0x00000001, 3, 8, },
  34. { "apic", 0x00000001, 3, 9, },
  35. { "sep", 0x00000001, 3, 11, },
  36. { "mtrr", 0x00000001, 3, 12, },
  37. { "pge", 0x00000001, 3, 13, },
  38. { "mca", 0x00000001, 3, 14, },
  39. { "cmov", 0x00000001, 3, 15, },
  40. { "pat", 0x00000001, 3, 16, },
  41. { "pse36", 0x00000001, 3, 17, },
  42. { "pn", 0x00000001, 3, 18, },
  43. { "clflush", 0x00000001, 3, 19, },
  44. { "dts", 0x00000001, 3, 21, },
  45. { "acpi", 0x00000001, 3, 22, },
  46. { "mmx", 0x00000001, 3, 23, },
  47. { "fxsr", 0x00000001, 3, 24, },
  48. { "sse", 0x00000001, 3, 25, },
  49. { "sse2", 0x00000001, 3, 26, },
  50. { "ss", 0x00000001, 3, 27, },
  51. { "ht", 0x00000001, 3, 28, },
  52. { "tm", 0x00000001, 3, 29, },
  53. { "ia64", 0x00000001, 3, 30, },
  54. { "pbe", 0x00000001, 3, 31, },
  55. { "pni", 0x00000001, 2, 0, },
  56. { "pclmulqdq", 0x00000001, 2, 1, },
  57. { "dtes64", 0x00000001, 2, 2, },
  58. { "monitor", 0x00000001, 2, 3, },
  59. { "ds_cpl", 0x00000001, 2, 4, },
  60. { "vmx", 0x00000001, 2, 5, },
  61. { "smx", 0x00000001, 2, 6, },
  62. { "est", 0x00000001, 2, 7, },
  63. { "tm2", 0x00000001, 2, 8, },
  64. { "ssse3", 0x00000001, 2, 9, },
  65. { "cid", 0x00000001, 2, 10, },
  66. { "sdbg", 0x00000001, 2, 11, },
  67. { "fma", 0x00000001, 2, 12, },
  68. { "cx16", 0x00000001, 2, 13, },
  69. { "xtpr", 0x00000001, 2, 14, },
  70. { "pdcm", 0x00000001, 2, 15, },
  71. { "pcid", 0x00000001, 2, 17, },
  72. { "dca", 0x00000001, 2, 18, },
  73. { "sse4_1", 0x00000001, 2, 19, },
  74. { "sse4_2", 0x00000001, 2, 20, },
  75. { "x2apic", 0x00000001, 2, 21, },
  76. { "movbe", 0x00000001, 2, 22, },
  77. { "popcnt", 0x00000001, 2, 23, },
  78. { "tsc_deadline_timer", 0x00000001, 2, 24, },
  79. { "aes", 0x00000001, 2, 25, },
  80. { "xsave", 0x00000001, 2, 26, },
  81. { "osxsave", 0x00000001, 2, 27, },
  82. { "avx", 0x00000001, 2, 28, },
  83. { "f16c", 0x00000001, 2, 29, },
  84. { "rdrand", 0x00000001, 2, 30, },
  85. { "hypervisor", 0x00000001, 2, 31, },
  86. { "lahf_lm", 0x80000001, 2, 0, },
  87. { "cmp_legacy", 0x80000001, 2, 1, },
  88. { "svm", 0x80000001, 2, 2, },
  89. { "extapic", 0x80000001, 2, 3, },
  90. { "cr8_legacy", 0x80000001, 2, 4, },
  91. { "abm", 0x80000001, 2, 5, },
  92. { "sse4a", 0x80000001, 2, 6, },
  93. { "misalignsse", 0x80000001, 2, 7, },
  94. { "3dnowprefetch", 0x80000001, 2, 8, },
  95. { "osvw", 0x80000001, 2, 9, },
  96. { "ibs", 0x80000001, 2, 10, },
  97. { "xop", 0x80000001, 2, 11, },
  98. { "skinit", 0x80000001, 2, 12, },
  99. { "wdt", 0x80000001, 2, 13, },
  100. { "lwp", 0x80000001, 2, 15, },
  101. { "fma4", 0x80000001, 2, 16, },
  102. { "tce", 0x80000001, 2, 17, },
  103. { "nodeid_msr", 0x80000001, 2, 19, },
  104. { "tbm", 0x80000001, 2, 21, },
  105. { "topoext", 0x80000001, 2, 22, },
  106. { "perfctr_core", 0x80000001, 2, 23, },
  107. { "perfctr_nb", 0x80000001, 2, 24, },
  108. { "bpext", 0x80000001, 2, 26, },
  109. { "ptsc", 0x80000001, 2, 27, },
  110. { "perfctr_llc", 0x80000001, 2, 28, },
  111. { "mwaitx", 0x80000001, 2, 29, },
  112. { "fsgsbase", 0x00000007, 1, 0, },
  113. { "tsc_adjust", 0x00000007, 1, 1, },
  114. { "bmi1", 0x00000007, 1, 3, },
  115. { "hle", 0x00000007, 1, 4, },
  116. { "avx2", 0x00000007, 1, 5, },
  117. { "smep", 0x00000007, 1, 7, },
  118. { "bmi2", 0x00000007, 1, 8, },
  119. { "erms", 0x00000007, 1, 9, },
  120. { "invpcid", 0x00000007, 1, 10, },
  121. { "rtm", 0x00000007, 1, 11, },
  122. { "cqm", 0x00000007, 1, 12, },
  123. { "mpx", 0x00000007, 1, 14, },
  124. { "rdt_a", 0x00000007, 1, 15, },
  125. { "avx512f", 0x00000007, 1, 16, },
  126. { "avx512dq", 0x00000007, 1, 17, },
  127. { "rdseed", 0x00000007, 1, 18, },
  128. { "adx", 0x00000007, 1, 19, },
  129. { "smap", 0x00000007, 1, 20, },
  130. { "avx512ifma", 0x00000007, 1, 21, },
  131. { "clflushopt", 0x00000007, 1, 23, },
  132. { "clwb", 0x00000007, 1, 24, },
  133. { "intel_pt", 0x00000007, 1, 25, },
  134. { "avx512pf", 0x00000007, 1, 26, },
  135. { "avx512er", 0x00000007, 1, 27, },
  136. { "avx512cd", 0x00000007, 1, 28, },
  137. { "sha_ni", 0x00000007, 1, 29, },
  138. { "avx512bw", 0x00000007, 1, 30, },
  139. { "avx512vl", 0x00000007, 1, 31, },
  140. { "xsaveopt", 0x0000000d, 0, 0, },
  141. { "xsavec", 0x0000000d, 0, 1, },
  142. { "xgetbv1", 0x0000000d, 0, 2, },
  143. { "xsaves", 0x0000000d, 0, 3, },
  144. { "cqm_llc", 0x0000000f, 3, 1, },
  145. { "cqm_occup_llc", 0x0000000f, 3, 0, },
  146. { "cqm_mbm_total", 0x0000000f, 3, 1, },
  147. { "cqm_mbm_local", 0x0000000f, 3, 2, },
  148. { "dtherm", 0x00000006, 1, 0, },
  149. { "ida", 0x00000006, 1, 1, },
  150. { "arat", 0x00000006, 1, 2, },
  151. { "pln", 0x00000006, 1, 4, },
  152. { "pts", 0x00000006, 1, 6, },
  153. { "hwp", 0x00000006, 1, 7, },
  154. { "hwp_notify", 0x00000006, 1, 8, },
  155. { "hwp_act_window", 0x00000006, 1, 9, },
  156. { "hwp_epp", 0x00000006, 1, 10, },
  157. { "hwp_pkg_req", 0x00000006, 1, 11, },
  158. { "avx512vbmi", 0x00000007, 2, 1, },
  159. { "umip", 0x00000007, 2, 2, },
  160. { "pku", 0x00000007, 2, 3, },
  161. { "ospke", 0x00000007, 2, 4, },
  162. { "avx512_vbmi2", 0x00000007, 2, 6, },
  163. { "gfni", 0x00000007, 2, 8, },
  164. { "vaes", 0x00000007, 2, 9, },
  165. { "vpclmulqdq", 0x00000007, 2, 10, },
  166. { "avx512_vnni", 0x00000007, 2, 11, },
  167. { "avx512_bitalg", 0x00000007, 2, 12, },
  168. { "tme", 0x00000007, 2, 13, },
  169. { "avx512_vpopcntdq", 0x00000007, 2, 14, },
  170. { "la57", 0x00000007, 2, 16, },
  171. { "rdpid", 0x00000007, 2, 22, },
  172. { "cldemote", 0x00000007, 2, 25, },
  173. { "movdiri", 0x00000007, 2, 27, },
  174. { "movdir64b", 0x00000007, 2, 28, },
  175. { "avx512_4vnniw", 0x00000007, 3, 2, },
  176. { "avx512_4fmaps", 0x00000007, 3, 3, },
  177. { "tsx_force_abort", 0x00000007, 3, 13, },
  178. { "pconfig", 0x00000007, 3, 18, },
  179. { "spec_ctrl", 0x00000007, 3, 26, },
  180. { "intel_stibp", 0x00000007, 3, 27, },
  181. { "flush_l1d", 0x00000007, 3, 28, },
  182. { "arch_capabilities", 0x00000007, 3, 29, },
  183. { "spec_ctrl_ssbd", 0x00000007, 3, 31, },
  184. };
  185. typedef struct IOMap IOMap;
  186. struct IOMap
  187. {
  188. IOMap *next;
  189. int reserved;
  190. char tag[13];
  191. uint32_t start;
  192. uint32_t end;
  193. };
  194. static struct
  195. {
  196. Lock l;
  197. IOMap *map;
  198. IOMap *free;
  199. IOMap maps[32]; // some initial free maps
  200. QLock ql; // lock for reading map
  201. } iomap;
  202. enum {
  203. Qdir = 0,
  204. Qioalloc = 1,
  205. Qiob,
  206. Qiow,
  207. Qiol,
  208. Qbase,
  209. Qmapram,
  210. Qmax = 32,
  211. };
  212. typedef int32_t Rdwrfn(Chan*, void*, int32_t, int64_t);
  213. static Rdwrfn *readfn[Qmax];
  214. static Rdwrfn *writefn[Qmax];
  215. static Dirtab archdir[Qmax] = {
  216. {".", { Qdir, 0, QTDIR }, 0, 0555},
  217. {"ioalloc", { Qioalloc, 0 }, 0, 0444},
  218. /* NOTE: kludge until we have real permissions. */
  219. {"iob", { Qiob, 0 }, 0, 0660 | 6},
  220. {"iow", { Qiow, 0 }, 0, 0660 | 6},
  221. {"iol", { Qiol, 0 }, 0, 0660 | 6},
  222. {"mapram", { Qmapram, 0 }, 0, 0444},
  223. };
  224. Lock archwlock; /* the lock is only for changing archdir */
  225. int narchdir = Qbase;
  226. /*
  227. * Add a file to the #P listing. Once added, you can't delete it.
  228. * You can't add a file with the same name as one already there,
  229. * and you get a pointer to the Dirtab entry so you can do things
  230. * like change the Qid version. Changing the Qid path is disallowed.
  231. */
  232. Dirtab*
  233. addarchfile(char *name, int perm, Rdwrfn *rdfn, Rdwrfn *wrfn)
  234. {
  235. int i;
  236. Dirtab d;
  237. Dirtab *dp;
  238. memset(&d, 0, sizeof d);
  239. strcpy(d.name, name);
  240. d.perm = perm;
  241. lock(&archwlock);
  242. if(narchdir >= Qmax){
  243. panic("addarchfile: %s: no more slots available: increase Qmax");
  244. }
  245. for(i=0; i<narchdir; i++)
  246. if(strcmp(archdir[i].name, name) == 0){
  247. unlock(&archwlock);
  248. return nil;
  249. }
  250. d.qid.path = narchdir;
  251. archdir[narchdir] = d;
  252. readfn[narchdir] = rdfn;
  253. writefn[narchdir] = wrfn;
  254. dp = &archdir[narchdir++];
  255. unlock(&archwlock);
  256. return dp;
  257. }
  258. void
  259. ioinit(void)
  260. {
  261. char *excluded;
  262. int i;
  263. for(i = 0; i < nelem(iomap.maps)-1; i++)
  264. iomap.maps[i].next = &iomap.maps[i+1];
  265. iomap.maps[i].next = nil;
  266. iomap.free = iomap.maps;
  267. /*
  268. * Someone needs to explain why this was here...
  269. */
  270. ioalloc(0x0fff, 1, 0, "dummy"); // i82557 is at 0x1000, the dummy
  271. // entry is needed for swappable devs.
  272. if (0) {// (excluded = getconf("ioexclude")) != nil) {
  273. char *s;
  274. s = excluded;
  275. while (s && *s != '\0' && *s != '\n') {
  276. char *ends;
  277. int io_s, io_e;
  278. io_s = (int)strtol(s, &ends, 0);
  279. if (ends == nil || ends == s || *ends != '-') {
  280. print("ioinit: cannot parse option string\n");
  281. break;
  282. }
  283. s = ++ends;
  284. io_e = (int)strtol(s, &ends, 0);
  285. if (ends && *ends == ',')
  286. *ends++ = '\0';
  287. s = ends;
  288. ioalloc(io_s, io_e - io_s + 1, 0, "pre-allocated");
  289. }
  290. }
  291. }
  292. // Reserve a range to be ioalloced later.
  293. // This is in particular useful for exchangable cards, such
  294. // as pcmcia and cardbus cards.
  295. int
  296. ioreserve(int n, int size, int align, char *tag)
  297. {
  298. IOMap *map, **l;
  299. int i, port;
  300. lock(&iomap.l);
  301. // find a free port above 0x400 and below 0x1000
  302. port = 0x400;
  303. for(l = &iomap.map; *l; l = &(*l)->next){
  304. map = *l;
  305. if (map->start < 0x400)
  306. continue;
  307. i = map->start - port;
  308. if(i > size)
  309. break;
  310. if(align > 0)
  311. port = ((port+align-1)/align)*align;
  312. else
  313. port = map->end;
  314. }
  315. if(*l == nil){
  316. unlock(&iomap.l);
  317. return -1;
  318. }
  319. map = iomap.free;
  320. if(map == nil){
  321. print("ioalloc: out of maps");
  322. unlock(&iomap.l);
  323. return port;
  324. }
  325. iomap.free = map->next;
  326. map->next = *l;
  327. map->start = port;
  328. map->end = port + size;
  329. map->reserved = 1;
  330. strncpy(map->tag, tag, sizeof(map->tag));
  331. map->tag[sizeof(map->tag)-1] = 0;
  332. *l = map;
  333. archdir[0].qid.vers++;
  334. unlock(&iomap.l);
  335. return map->start;
  336. }
  337. //
  338. // alloc some io port space and remember who it was
  339. // alloced to. if port < 0, find a free region.
  340. //
  341. int
  342. ioalloc(int port, int size, int align, char *tag)
  343. {
  344. IOMap *map, **l;
  345. int i;
  346. lock(&iomap.l);
  347. if(port < 0){
  348. // find a free port above 0x400 and below 0x1000
  349. port = 0x400;
  350. for(l = &iomap.map; *l; l = &(*l)->next){
  351. map = *l;
  352. if (map->start < 0x400)
  353. continue;
  354. i = map->start - port;
  355. if(i > size)
  356. break;
  357. if(align > 0)
  358. port = ((port+align-1)/align)*align;
  359. else
  360. port = map->end;
  361. }
  362. if(*l == nil){
  363. unlock(&iomap.l);
  364. return -1;
  365. }
  366. } else {
  367. // Only 64KB I/O space on the x86.
  368. if((port+size) > 0x10000){
  369. unlock(&iomap.l);
  370. return -1;
  371. }
  372. // see if the space clashes with previously allocated ports
  373. for(l = &iomap.map; *l; l = &(*l)->next){
  374. map = *l;
  375. if(map->end <= port)
  376. continue;
  377. if(map->reserved && map->start == port && map->end == port + size) {
  378. map->reserved = 0;
  379. unlock(&iomap.l);
  380. return map->start;
  381. }
  382. if(map->start >= port+size)
  383. break;
  384. unlock(&iomap.l);
  385. return -1;
  386. }
  387. }
  388. map = iomap.free;
  389. if(map == nil){
  390. print("ioalloc: out of maps");
  391. unlock(&iomap.l);
  392. return port;
  393. }
  394. iomap.free = map->next;
  395. map->next = *l;
  396. map->start = port;
  397. map->end = port + size;
  398. strncpy(map->tag, tag, sizeof(map->tag));
  399. map->tag[sizeof(map->tag)-1] = 0;
  400. *l = map;
  401. archdir[0].qid.vers++;
  402. unlock(&iomap.l);
  403. return map->start;
  404. }
  405. void
  406. iofree(int port)
  407. {
  408. IOMap *map, **l;
  409. lock(&iomap.l);
  410. for(l = &iomap.map; *l; l = &(*l)->next){
  411. if((*l)->start == port){
  412. map = *l;
  413. *l = map->next;
  414. map->next = iomap.free;
  415. iomap.free = map;
  416. break;
  417. }
  418. if((*l)->start > port)
  419. break;
  420. }
  421. archdir[0].qid.vers++;
  422. unlock(&iomap.l);
  423. }
  424. int
  425. iounused(int start, int end)
  426. {
  427. IOMap *map;
  428. for(map = iomap.map; map != nil; map = map->next){
  429. if((start >= map->start && start < map->end)
  430. || (start <= map->start && end > map->start))
  431. return 0;
  432. }
  433. return 1;
  434. }
  435. static void
  436. checkport(int start, int end)
  437. {
  438. /* standard vga regs are OK */
  439. if(start >= 0x2b0 && end <= 0x2df+1)
  440. return;
  441. if(start >= 0x3c0 && end <= 0x3da+1)
  442. return;
  443. if(iounused(start, end))
  444. return;
  445. error(Eperm);
  446. }
  447. static Chan*
  448. archattach(char* spec)
  449. {
  450. return devattach('P', spec);
  451. }
  452. Walkqid*
  453. archwalk(Chan* c, Chan *nc, char** name, int nname)
  454. {
  455. return devwalk(c, nc, name, nname, archdir, narchdir, devgen);
  456. }
  457. static int32_t
  458. archstat(Chan* c, uint8_t* dp, int32_t n)
  459. {
  460. return devstat(c, dp, n, archdir, narchdir, devgen);
  461. }
  462. static Chan*
  463. archopen(Chan* c, int omode)
  464. {
  465. return devopen(c, omode, archdir, narchdir, devgen);
  466. }
  467. static void
  468. archclose(Chan* c)
  469. {
  470. }
  471. enum
  472. {
  473. Linelen= 31,
  474. };
  475. static int32_t
  476. archread(Chan *c, void *a, int32_t n, int64_t offset)
  477. {
  478. char *buf, *p;
  479. int port;
  480. uint16_t *sp;
  481. uint32_t *lp;
  482. IOMap *map;
  483. Rdwrfn *fn;
  484. switch((uint32_t)c->qid.path){
  485. case Qdir:
  486. return devdirread(c, a, n, archdir, narchdir, devgen);
  487. case Qiob:
  488. port = offset;
  489. checkport(offset, offset+n);
  490. for(p = a; port < offset+n; port++)
  491. *p++ = inb(port);
  492. return n;
  493. case Qiow:
  494. if(n & 1)
  495. error(Ebadarg);
  496. checkport(offset, offset+n);
  497. sp = a;
  498. for(port = offset; port < offset+n; port += 2)
  499. *sp++ = ins(port);
  500. return n;
  501. case Qiol:
  502. if(n & 3)
  503. error(Ebadarg);
  504. checkport(offset, offset+n);
  505. lp = a;
  506. for(port = offset; port < offset+n; port += 4)
  507. *lp++ = inl(port);
  508. return n;
  509. case Qioalloc:
  510. break;
  511. default:
  512. if(c->qid.path < narchdir && (fn = readfn[c->qid.path]))
  513. return fn(c, a, n, offset);
  514. error(Eperm);
  515. break;
  516. }
  517. if((buf = malloc(n)) == nil)
  518. error(Enomem);
  519. p = buf;
  520. n = n/Linelen;
  521. offset = offset/Linelen;
  522. switch((uint32_t)c->qid.path){
  523. case Qioalloc:
  524. lock(&iomap.l);
  525. for(map = iomap.map; n > 0 && map != nil; map = map->next){
  526. if(offset-- > 0)
  527. continue;
  528. sprint(p, "%#8lx %#8lx %-12.12s\n", map->start, map->end-1, map->tag);
  529. p += Linelen;
  530. n--;
  531. }
  532. unlock(&iomap.l);
  533. break;
  534. case Qmapram:
  535. /* shit */
  536. #ifdef NOTYET
  537. for(mp = rmapram.map; mp->size; mp++){
  538. /*
  539. * Up to MemMinMiB is already set up.
  540. */
  541. if(mp->addr < MemMinMiB*MiB){
  542. if(mp->addr+mp->size <= MemMinMiB*MiB)
  543. continue;
  544. pa = MemMinMiB*MiB;
  545. size = mp->size - MemMinMiB*MiB-mp->addr;
  546. }
  547. else{
  548. pa = mp->addr;
  549. size = mp->size;
  550. }
  551. }
  552. #endif
  553. error("Not yet");
  554. break;
  555. }
  556. n = p - buf;
  557. memmove(a, buf, n);
  558. free(buf);
  559. return n;
  560. }
  561. static int32_t
  562. archwrite(Chan *c, void *a, int32_t n, int64_t offset)
  563. {
  564. char *p;
  565. int port;
  566. uint16_t *sp;
  567. uint32_t *lp;
  568. Rdwrfn *fn;
  569. switch((uint32_t)c->qid.path){
  570. case Qiob:
  571. p = a;
  572. checkport(offset, offset+n);
  573. for(port = offset; port < offset+n; port++)
  574. outb(port, *p++);
  575. return n;
  576. case Qiow:
  577. if(n & 1)
  578. error(Ebadarg);
  579. checkport(offset, offset+n);
  580. sp = a;
  581. for(port = offset; port < offset+n; port += 2)
  582. outs(port, *sp++);
  583. return n;
  584. case Qiol:
  585. if(n & 3)
  586. error(Ebadarg);
  587. checkport(offset, offset+n);
  588. lp = a;
  589. for(port = offset; port < offset+n; port += 4)
  590. outl(port, *lp++);
  591. return n;
  592. default:
  593. if(c->qid.path < narchdir && (fn = writefn[c->qid.path]))
  594. return fn(c, a, n, offset);
  595. error(Eperm);
  596. break;
  597. }
  598. return 0;
  599. }
  600. Dev archdevtab = {
  601. .dc = 'P',
  602. .name = "arch",
  603. .reset = devreset,
  604. .init = devinit,
  605. .shutdown = devshutdown,
  606. .attach = archattach,
  607. .walk = archwalk,
  608. .stat = archstat,
  609. .open = archopen,
  610. .create = devcreate,
  611. .close = archclose,
  612. .read = archread,
  613. .bread = devbread,
  614. .write = archwrite,
  615. .bwrite = devbwrite,
  616. .remove = devremove,
  617. .wstat = devwstat,
  618. };
  619. /*
  620. */
  621. void
  622. nop(void)
  623. {
  624. }
  625. void (*coherence)(void) = mfence;
  626. static int32_t
  627. cputyperead(Chan* c, void *a, int32_t n, int64_t off)
  628. {
  629. char buf[512], *s, *e;
  630. char *vendorid;
  631. uint32_t info0[4];
  632. s = buf;
  633. e = buf+sizeof buf;
  634. if(!cpuidinfo(0, 0, info0)) {
  635. iprint("cputyperead: cpuidinfo(0, 0) failed, switching to default\n");
  636. vendorid = "Generic X86_64";
  637. } else
  638. vendorid = cpuidname(info0);
  639. s = seprint(s, e, "%s CPU @ %uMHz\ncpu cores: %d\n", vendorid, machp()->cpumhz, sys->nmach);
  640. return readstr(off, a, n, buf);
  641. }
  642. static void
  643. get_cpuid_limits(int *num_basic, int *num_hypervisor, int *num_extended)
  644. {
  645. uint32_t info[4];
  646. *num_basic = 0;
  647. *num_hypervisor = 0;
  648. *num_extended = 0;
  649. if (cpuid(0x00000000, 0, info)) {
  650. *num_basic = info[0] + 1;
  651. }
  652. if (cpuid(0x40000000, 0, info)) {
  653. *num_hypervisor = info[0] - 0x40000000 + 1;
  654. }
  655. if (cpuid(0x80000000, 0, info)) {
  656. *num_extended = info[0] - 0x80000000 + 1;
  657. }
  658. }
  659. // Given an index into the valid cpuids, and the number of each range of values,
  660. // return the appropriate EAX value.
  661. static int32_t
  662. itoeax(int i, uint32_t num_basic, uint32_t num_hyp, uint32_t num_ext) {
  663. uint32_t first_hyp = num_basic;
  664. uint32_t first_ext = num_basic + num_hyp;
  665. if (i >= first_ext) {
  666. return 0x80000000 + i - first_ext;
  667. } else if (i >= first_hyp) {
  668. return 0x40000000 + i - first_hyp;
  669. } else {
  670. return i;
  671. }
  672. }
  673. // Output hex values of all valid cpuid values
  674. static int32_t
  675. cpuidrawread(Chan* c, void *a, int32_t n, int64_t off)
  676. {
  677. char buf[4096];
  678. char *s = buf;
  679. char *e = buf+sizeof buf;
  680. uint32_t info[4];
  681. int num_basic = 0, num_hyp = 0, num_ext = 0;
  682. get_cpuid_limits(&num_basic, &num_hyp, &num_ext);
  683. for (int i = 0; i < num_basic + num_hyp + num_ext; i++) {
  684. uint32_t eax = itoeax(i, num_basic, num_hyp, num_ext);
  685. if (!cpuid(eax, 0, info)) {
  686. continue;
  687. }
  688. s = seprint(s, e, "%#8.8x %#8.8x %#8.8x %#8.8x %#8.8x %#8.8x\n",
  689. eax, 0, info[0], info[1], info[2], info[3]);
  690. }
  691. return readstr(off, a, n, buf);
  692. }
  693. // Output cpu flag shortnames from cpuid values
  694. static int32_t
  695. cpuidflagsread(Chan* c, void *a, int32_t n, int64_t off)
  696. {
  697. char buf[4096];
  698. char *s = buf;
  699. char *e = buf+sizeof buf;
  700. uint32_t info[4];
  701. int num_basic = 0, num_hyp = 0, num_ext = 0;
  702. get_cpuid_limits(&num_basic, &num_hyp, &num_ext);
  703. int num_flags = nelem(cpuflags);
  704. for (int i = 0; i < num_basic + num_hyp + num_ext; i++) {
  705. uint32_t eax = itoeax(i, num_basic, num_hyp, num_ext);
  706. if (!cpuid(eax, 0, info)) {
  707. continue;
  708. }
  709. // Extract any flag names if this particular eax contains flags
  710. for (int fi = 0; fi < num_flags; fi++) {
  711. Cpuflag *flag = &cpuflags[fi];
  712. if (flag->eax != eax) {
  713. continue;
  714. }
  715. if (info[flag->infoidx] & (1 << flag->bitidx)) {
  716. s = seprint(s, e, "%s ", flag->name);
  717. }
  718. }
  719. }
  720. s = seprint(s, e, "\n");
  721. return readstr(off, a, n, buf);
  722. }
  723. void
  724. archinit(void)
  725. {
  726. addarchfile("cputype", 0444, cputyperead, nil);
  727. addarchfile("mtags", 0444, mtagsread, nil);
  728. addarchfile("cpuidraw", 0444, cpuidrawread, nil);
  729. addarchfile("cpuidflags", 0444, cpuidflagsread, nil);
  730. }
  731. void
  732. archreset(void)
  733. {
  734. int i;
  735. /*
  736. * And sometimes there is no keyboard...
  737. *
  738. * The reset register (0xcf9) is usually in one of the bridge
  739. * chips. The actual location and sequence could be extracted from
  740. * ACPI but why bother, this is the end of the line anyway.
  741. print("Takes a licking and keeps on ticking...\n");
  742. */
  743. i = inb(0xcf9); /* ICHx reset control */
  744. i &= 0x06;
  745. outb(0xcf9, i|0x02); /* SYS_RST */
  746. millidelay(1);
  747. outb(0xcf9, i|0x06); /* RST_CPU transition */
  748. for(;;)
  749. ;
  750. }
  751. /*
  752. * return value and speed of timer
  753. */
  754. uint64_t
  755. fastticks(uint64_t* hz)
  756. {
  757. if(hz != nil)
  758. *hz = machp()->cpuhz;
  759. return rdtsc();
  760. }
  761. uint32_t
  762. ms(void)
  763. {
  764. return fastticks2us(rdtsc());
  765. }
  766. /*
  767. * set next timer interrupt
  768. */
  769. void
  770. timerset(uint64_t x)
  771. {
  772. extern void apictimerset(uint64_t);
  773. apictimerset(x);
  774. }
  775. void
  776. cycles(uint64_t* t)
  777. {
  778. *t = rdtsc();
  779. }
  780. void
  781. delay(int millisecs)
  782. {
  783. uint64_t r, t;
  784. if(millisecs <= 0)
  785. millisecs = 1;
  786. r = rdtsc();
  787. for(t = r + (sys->cyclefreq*millisecs)/1000ull; r < t; r = rdtsc())
  788. ;
  789. }
  790. /*
  791. * performance measurement ticks. must be low overhead.
  792. * doesn't have to count over a second.
  793. */
  794. uint64_t
  795. perfticks(void)
  796. {
  797. uint64_t x;
  798. // if(m->havetsc)
  799. cycles(&x);
  800. // else
  801. // x = 0;
  802. return x;
  803. }