extmem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * File...........: arch/s390/mm/extmem.c
  3. * Author(s)......: Carsten Otte <cotte@de.ibm.com>
  4. * Rob M van der Heij <rvdheij@nl.ibm.com>
  5. * Steven Shultz <shultzss@us.ibm.com>
  6. * Bugreports.to..: <Linux390@de.ibm.com>
  7. * (C) IBM Corporation 2002-2004
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/string.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/bootmem.h>
  16. #include <linux/ctype.h>
  17. #include <asm/page.h>
  18. #include <asm/ebcdic.h>
  19. #include <asm/errno.h>
  20. #include <asm/extmem.h>
  21. #include <asm/cpcmd.h>
  22. #include <asm/setup.h>
  23. #define DCSS_DEBUG /* Debug messages on/off */
  24. #define DCSS_NAME "extmem"
  25. #ifdef DCSS_DEBUG
  26. #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSS_NAME " debug:" x)
  27. #else
  28. #define PRINT_DEBUG(x...) do {} while (0)
  29. #endif
  30. #define PRINT_INFO(x...) printk(KERN_INFO DCSS_NAME " info:" x)
  31. #define PRINT_WARN(x...) printk(KERN_WARNING DCSS_NAME " warning:" x)
  32. #define PRINT_ERR(x...) printk(KERN_ERR DCSS_NAME " error:" x)
  33. #define DCSS_LOADSHR 0x00
  34. #define DCSS_LOADNSR 0x04
  35. #define DCSS_PURGESEG 0x08
  36. #define DCSS_FINDSEG 0x0c
  37. #define DCSS_LOADNOLY 0x10
  38. #define DCSS_SEGEXT 0x18
  39. #define DCSS_FINDSEGA 0x0c
  40. struct qrange {
  41. unsigned int start; // 3byte start address, 1 byte type
  42. unsigned int end; // 3byte end address, 1 byte reserved
  43. };
  44. struct qout64 {
  45. int segstart;
  46. int segend;
  47. int segcnt;
  48. int segrcnt;
  49. struct qrange range[6];
  50. };
  51. struct qin64 {
  52. char qopcode;
  53. char rsrv1[3];
  54. char qrcode;
  55. char rsrv2[3];
  56. char qname[8];
  57. unsigned int qoutptr;
  58. short int qoutlen;
  59. };
  60. struct dcss_segment {
  61. struct list_head list;
  62. char dcss_name[8];
  63. unsigned long start_addr;
  64. unsigned long end;
  65. atomic_t ref_count;
  66. int do_nonshared;
  67. unsigned int vm_segtype;
  68. struct qrange range[6];
  69. int segcnt;
  70. };
  71. static DEFINE_SPINLOCK(dcss_lock);
  72. static struct list_head dcss_list = LIST_HEAD_INIT(dcss_list);
  73. static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
  74. "EW/EN-MIXED" };
  75. /*
  76. * Create the 8 bytes, ebcdic VM segment name from
  77. * an ascii name.
  78. */
  79. static void inline
  80. dcss_mkname(char *name, char *dcss_name)
  81. {
  82. int i;
  83. for (i = 0; i < 8; i++) {
  84. if (name[i] == '\0')
  85. break;
  86. dcss_name[i] = toupper(name[i]);
  87. };
  88. for (; i < 8; i++)
  89. dcss_name[i] = ' ';
  90. ASCEBC(dcss_name, 8);
  91. }
  92. /*
  93. * search all segments in dcss_list, and return the one
  94. * namend *name. If not found, return NULL.
  95. */
  96. static struct dcss_segment *
  97. segment_by_name (char *name)
  98. {
  99. char dcss_name[9];
  100. struct list_head *l;
  101. struct dcss_segment *tmp, *retval = NULL;
  102. assert_spin_locked(&dcss_lock);
  103. dcss_mkname (name, dcss_name);
  104. list_for_each (l, &dcss_list) {
  105. tmp = list_entry (l, struct dcss_segment, list);
  106. if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
  107. retval = tmp;
  108. break;
  109. }
  110. }
  111. return retval;
  112. }
  113. /*
  114. * Perform a function on a dcss segment.
  115. */
  116. static inline int
  117. dcss_diag (__u8 func, void *parameter,
  118. unsigned long *ret1, unsigned long *ret2)
  119. {
  120. unsigned long rx, ry;
  121. int rc;
  122. rx = (unsigned long) parameter;
  123. ry = (unsigned long) func;
  124. asm volatile(
  125. #ifdef CONFIG_64BIT
  126. " sam31\n"
  127. " diag %0,%1,0x64\n"
  128. " sam64\n"
  129. #else
  130. " diag %0,%1,0x64\n"
  131. #endif
  132. " ipm %2\n"
  133. " srl %2,28\n"
  134. : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
  135. *ret1 = rx;
  136. *ret2 = ry;
  137. return rc;
  138. }
  139. static inline int
  140. dcss_diag_translate_rc (int vm_rc) {
  141. if (vm_rc == 44)
  142. return -ENOENT;
  143. return -EIO;
  144. }
  145. /* do a diag to get info about a segment.
  146. * fills start_address, end and vm_segtype fields
  147. */
  148. static int
  149. query_segment_type (struct dcss_segment *seg)
  150. {
  151. struct qin64 *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
  152. struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
  153. int diag_cc, rc, i;
  154. unsigned long dummy, vmrc;
  155. if ((qin == NULL) || (qout == NULL)) {
  156. rc = -ENOMEM;
  157. goto out_free;
  158. }
  159. /* initialize diag input parameters */
  160. qin->qopcode = DCSS_FINDSEGA;
  161. qin->qoutptr = (unsigned long) qout;
  162. qin->qoutlen = sizeof(struct qout64);
  163. memcpy (qin->qname, seg->dcss_name, 8);
  164. diag_cc = dcss_diag (DCSS_SEGEXT, qin, &dummy, &vmrc);
  165. if (diag_cc > 1) {
  166. PRINT_WARN ("segment_type: diag returned error %ld\n", vmrc);
  167. rc = dcss_diag_translate_rc (vmrc);
  168. goto out_free;
  169. }
  170. if (qout->segcnt > 6) {
  171. rc = -ENOTSUPP;
  172. goto out_free;
  173. }
  174. if (qout->segcnt == 1) {
  175. seg->vm_segtype = qout->range[0].start & 0xff;
  176. } else {
  177. /* multi-part segment. only one type supported here:
  178. - all parts are contiguous
  179. - all parts are either EW or EN type
  180. - maximum 6 parts allowed */
  181. unsigned long start = qout->segstart >> PAGE_SHIFT;
  182. for (i=0; i<qout->segcnt; i++) {
  183. if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
  184. ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
  185. rc = -ENOTSUPP;
  186. goto out_free;
  187. }
  188. if (start != qout->range[i].start >> PAGE_SHIFT) {
  189. rc = -ENOTSUPP;
  190. goto out_free;
  191. }
  192. start = (qout->range[i].end >> PAGE_SHIFT) + 1;
  193. }
  194. seg->vm_segtype = SEG_TYPE_EWEN;
  195. }
  196. /* analyze diag output and update seg */
  197. seg->start_addr = qout->segstart;
  198. seg->end = qout->segend;
  199. memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
  200. seg->segcnt = qout->segcnt;
  201. rc = 0;
  202. out_free:
  203. kfree(qin);
  204. kfree(qout);
  205. return rc;
  206. }
  207. /*
  208. * check if the given segment collides with guest storage.
  209. * returns 1 if this is the case, 0 if no collision was found
  210. */
  211. static int
  212. segment_overlaps_storage(struct dcss_segment *seg)
  213. {
  214. int i;
  215. for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
  216. if (memory_chunk[i].type != CHUNK_READ_WRITE)
  217. continue;
  218. if ((memory_chunk[i].addr >> 20) > (seg->end >> 20))
  219. continue;
  220. if (((memory_chunk[i].addr + memory_chunk[i].size - 1) >> 20)
  221. < (seg->start_addr >> 20))
  222. continue;
  223. return 1;
  224. }
  225. return 0;
  226. }
  227. /*
  228. * check if segment collides with other segments that are currently loaded
  229. * returns 1 if this is the case, 0 if no collision was found
  230. */
  231. static int
  232. segment_overlaps_others (struct dcss_segment *seg)
  233. {
  234. struct list_head *l;
  235. struct dcss_segment *tmp;
  236. assert_spin_locked(&dcss_lock);
  237. list_for_each(l, &dcss_list) {
  238. tmp = list_entry(l, struct dcss_segment, list);
  239. if ((tmp->start_addr >> 20) > (seg->end >> 20))
  240. continue;
  241. if ((tmp->end >> 20) < (seg->start_addr >> 20))
  242. continue;
  243. if (seg == tmp)
  244. continue;
  245. return 1;
  246. }
  247. return 0;
  248. }
  249. /*
  250. * check if segment exceeds the kernel mapping range (detected or set via mem=)
  251. * returns 1 if this is the case, 0 if segment fits into the range
  252. */
  253. static inline int
  254. segment_exceeds_range (struct dcss_segment *seg)
  255. {
  256. int seg_last_pfn = (seg->end) >> PAGE_SHIFT;
  257. if (seg_last_pfn > max_pfn)
  258. return 1;
  259. return 0;
  260. }
  261. /*
  262. * get info about a segment
  263. * possible return values:
  264. * -ENOSYS : we are not running on VM
  265. * -EIO : could not perform query diagnose
  266. * -ENOENT : no such segment
  267. * -ENOTSUPP: multi-part segment cannot be used with linux
  268. * -ENOSPC : segment cannot be used (overlaps with storage)
  269. * -ENOMEM : out of memory
  270. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  271. */
  272. int
  273. segment_type (char* name)
  274. {
  275. int rc;
  276. struct dcss_segment seg;
  277. if (!MACHINE_IS_VM)
  278. return -ENOSYS;
  279. dcss_mkname(name, seg.dcss_name);
  280. rc = query_segment_type (&seg);
  281. if (rc < 0)
  282. return rc;
  283. return seg.vm_segtype;
  284. }
  285. /*
  286. * real segment loading function, called from segment_load
  287. */
  288. static int
  289. __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
  290. {
  291. struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
  292. GFP_DMA);
  293. int dcss_command, rc, diag_cc;
  294. if (seg == NULL) {
  295. rc = -ENOMEM;
  296. goto out;
  297. }
  298. dcss_mkname (name, seg->dcss_name);
  299. rc = query_segment_type (seg);
  300. if (rc < 0)
  301. goto out_free;
  302. if (segment_exceeds_range(seg)) {
  303. PRINT_WARN ("segment_load: not loading segment %s - exceeds"
  304. " kernel mapping range\n",name);
  305. rc = -ERANGE;
  306. goto out_free;
  307. }
  308. if (segment_overlaps_storage(seg)) {
  309. PRINT_WARN ("segment_load: not loading segment %s - overlaps"
  310. " storage\n",name);
  311. rc = -ENOSPC;
  312. goto out_free;
  313. }
  314. if (segment_overlaps_others(seg)) {
  315. PRINT_WARN ("segment_load: not loading segment %s - overlaps"
  316. " other segments\n",name);
  317. rc = -EBUSY;
  318. goto out_free;
  319. }
  320. if (do_nonshared)
  321. dcss_command = DCSS_LOADNSR;
  322. else
  323. dcss_command = DCSS_LOADNOLY;
  324. diag_cc = dcss_diag(dcss_command, seg->dcss_name,
  325. &seg->start_addr, &seg->end);
  326. if (diag_cc > 1) {
  327. PRINT_WARN ("segment_load: could not load segment %s - "
  328. "diag returned error (%ld)\n",name,seg->end);
  329. rc = dcss_diag_translate_rc (seg->end);
  330. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  331. &seg->start_addr, &seg->end);
  332. goto out_free;
  333. }
  334. seg->do_nonshared = do_nonshared;
  335. atomic_set(&seg->ref_count, 1);
  336. list_add(&seg->list, &dcss_list);
  337. rc = seg->vm_segtype;
  338. *addr = seg->start_addr;
  339. *end = seg->end;
  340. if (do_nonshared)
  341. PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
  342. "type %s in non-shared mode\n", name,
  343. (void*)seg->start_addr, (void*)seg->end,
  344. segtype_string[seg->vm_segtype]);
  345. else
  346. PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
  347. "type %s in shared mode\n", name,
  348. (void*)seg->start_addr, (void*)seg->end,
  349. segtype_string[seg->vm_segtype]);
  350. goto out;
  351. out_free:
  352. kfree(seg);
  353. out:
  354. return rc;
  355. }
  356. /*
  357. * this function loads a DCSS segment
  358. * name : name of the DCSS
  359. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  360. * 1 indicates that the dcss should be exclusive for this linux image
  361. * addr : will be filled with start address of the segment
  362. * end : will be filled with end address of the segment
  363. * return values:
  364. * -ENOSYS : we are not running on VM
  365. * -EIO : could not perform query or load diagnose
  366. * -ENOENT : no such segment
  367. * -ENOTSUPP: multi-part segment cannot be used with linux
  368. * -ENOSPC : segment cannot be used (overlaps with storage)
  369. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  370. * -ERANGE : segment cannot be used (exceeds kernel mapping range)
  371. * -EPERM : segment is currently loaded with incompatible permissions
  372. * -ENOMEM : out of memory
  373. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  374. */
  375. int
  376. segment_load (char *name, int do_nonshared, unsigned long *addr,
  377. unsigned long *end)
  378. {
  379. struct dcss_segment *seg;
  380. int rc;
  381. if (!MACHINE_IS_VM)
  382. return -ENOSYS;
  383. spin_lock (&dcss_lock);
  384. seg = segment_by_name (name);
  385. if (seg == NULL)
  386. rc = __segment_load (name, do_nonshared, addr, end);
  387. else {
  388. if (do_nonshared == seg->do_nonshared) {
  389. atomic_inc(&seg->ref_count);
  390. *addr = seg->start_addr;
  391. *end = seg->end;
  392. rc = seg->vm_segtype;
  393. } else {
  394. *addr = *end = 0;
  395. rc = -EPERM;
  396. }
  397. }
  398. spin_unlock (&dcss_lock);
  399. return rc;
  400. }
  401. /*
  402. * this function modifies the shared state of a DCSS segment. note that
  403. * name : name of the DCSS
  404. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  405. * 1 indicates that the dcss should be exclusive for this linux image
  406. * return values:
  407. * -EIO : could not perform load diagnose (segment gone!)
  408. * -ENOENT : no such segment (segment gone!)
  409. * -EAGAIN : segment is in use by other exploiters, try later
  410. * -EINVAL : no segment with the given name is currently loaded - name invalid
  411. * 0 : operation succeeded
  412. */
  413. int
  414. segment_modify_shared (char *name, int do_nonshared)
  415. {
  416. struct dcss_segment *seg;
  417. unsigned long dummy;
  418. int dcss_command, rc, diag_cc;
  419. spin_lock (&dcss_lock);
  420. seg = segment_by_name (name);
  421. if (seg == NULL) {
  422. rc = -EINVAL;
  423. goto out_unlock;
  424. }
  425. if (do_nonshared == seg->do_nonshared) {
  426. PRINT_INFO ("segment_modify_shared: not reloading segment %s"
  427. " - already in requested mode\n",name);
  428. rc = 0;
  429. goto out_unlock;
  430. }
  431. if (atomic_read (&seg->ref_count) != 1) {
  432. PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
  433. "segment is in use by other driver(s)\n",name);
  434. rc = -EAGAIN;
  435. goto out_unlock;
  436. }
  437. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  438. &dummy, &dummy);
  439. if (do_nonshared)
  440. dcss_command = DCSS_LOADNSR;
  441. else
  442. dcss_command = DCSS_LOADNOLY;
  443. diag_cc = dcss_diag(dcss_command, seg->dcss_name,
  444. &seg->start_addr, &seg->end);
  445. if (diag_cc > 1) {
  446. PRINT_WARN ("segment_modify_shared: could not reload segment %s"
  447. " - diag returned error (%ld)\n",name,seg->end);
  448. rc = dcss_diag_translate_rc (seg->end);
  449. goto out_del;
  450. }
  451. seg->do_nonshared = do_nonshared;
  452. rc = 0;
  453. goto out_unlock;
  454. out_del:
  455. list_del(&seg->list);
  456. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  457. &dummy, &dummy);
  458. kfree(seg);
  459. out_unlock:
  460. spin_unlock(&dcss_lock);
  461. return rc;
  462. }
  463. /*
  464. * Decrease the use count of a DCSS segment and remove
  465. * it from the address space if nobody is using it
  466. * any longer.
  467. */
  468. void
  469. segment_unload(char *name)
  470. {
  471. unsigned long dummy;
  472. struct dcss_segment *seg;
  473. if (!MACHINE_IS_VM)
  474. return;
  475. spin_lock(&dcss_lock);
  476. seg = segment_by_name (name);
  477. if (seg == NULL) {
  478. PRINT_ERR ("could not find segment %s in segment_unload, "
  479. "please report to linux390@de.ibm.com\n",name);
  480. goto out_unlock;
  481. }
  482. if (atomic_dec_return(&seg->ref_count) == 0) {
  483. list_del(&seg->list);
  484. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  485. &dummy, &dummy);
  486. kfree(seg);
  487. }
  488. out_unlock:
  489. spin_unlock(&dcss_lock);
  490. }
  491. /*
  492. * save segment content permanently
  493. */
  494. void
  495. segment_save(char *name)
  496. {
  497. struct dcss_segment *seg;
  498. int startpfn = 0;
  499. int endpfn = 0;
  500. char cmd1[160];
  501. char cmd2[80];
  502. int i, response;
  503. if (!MACHINE_IS_VM)
  504. return;
  505. spin_lock(&dcss_lock);
  506. seg = segment_by_name (name);
  507. if (seg == NULL) {
  508. PRINT_ERR("could not find segment %s in segment_save, please "
  509. "report to linux390@de.ibm.com\n", name);
  510. goto out;
  511. }
  512. startpfn = seg->start_addr >> PAGE_SHIFT;
  513. endpfn = (seg->end) >> PAGE_SHIFT;
  514. sprintf(cmd1, "DEFSEG %s", name);
  515. for (i=0; i<seg->segcnt; i++) {
  516. sprintf(cmd1+strlen(cmd1), " %X-%X %s",
  517. seg->range[i].start >> PAGE_SHIFT,
  518. seg->range[i].end >> PAGE_SHIFT,
  519. segtype_string[seg->range[i].start & 0xff]);
  520. }
  521. sprintf(cmd2, "SAVESEG %s", name);
  522. response = 0;
  523. cpcmd(cmd1, NULL, 0, &response);
  524. if (response) {
  525. PRINT_ERR("segment_save: DEFSEG failed with response code %i\n",
  526. response);
  527. goto out;
  528. }
  529. cpcmd(cmd2, NULL, 0, &response);
  530. if (response) {
  531. PRINT_ERR("segment_save: SAVESEG failed with response code %i\n",
  532. response);
  533. goto out;
  534. }
  535. out:
  536. spin_unlock(&dcss_lock);
  537. }
  538. EXPORT_SYMBOL(segment_load);
  539. EXPORT_SYMBOL(segment_unload);
  540. EXPORT_SYMBOL(segment_save);
  541. EXPORT_SYMBOL(segment_type);
  542. EXPORT_SYMBOL(segment_modify_shared);