extmem.c 13 KB

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