graphene-ipc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. #include <linux/init.h>
  2. #include <linux/fs.h>
  3. #include <linux/mm.h>
  4. #include <linux/module.h>
  5. #include <linux/slab.h>
  6. #include <linux/miscdevice.h>
  7. #include <linux/kallsyms.h>
  8. #include <linux/sched.h>
  9. #include <linux/dcache.h>
  10. #include <linux/namei.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/version.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/bitmap.h>
  15. #include <asm/mman.h>
  16. #ifdef CONFIG_GRAPHENE_BULK_IPC
  17. # include "graphene.h"
  18. #else
  19. # include "my_tlb.h"
  20. # include "my_mmap.h"
  21. #endif
  22. #include "graphene-ipc.h"
  23. MODULE_LICENSE("Dual BSD/GPL");
  24. #define FILE_POISON LIST_POISON1
  25. struct kmem_cache *gipc_queue_cachep;
  26. struct kmem_cache *gipc_buf_cachep;
  27. #define GIPC_DEBUG 0
  28. #if defined(GIPC_DEBUG) && GIPC_DEBUG == 1
  29. # define DEBUG(...) printk(KERN_INFO __VA_ARGS__)
  30. # define GIPC_BUG_ON(cond) BUG_ON(cond)
  31. #else
  32. # define DEBUG(...)
  33. # define GIPC_BUG_ON(cond)
  34. #endif
  35. #ifndef gipc_get_session
  36. u32 (*my_gipc_get_session) (struct task_struct *) = NULL;
  37. #endif
  38. /* The page bufs going one direction */
  39. struct gipc_pages {
  40. struct file *file; // indicates if the struct is taken or not
  41. volatile int next, last;
  42. struct gipc_page_buf {
  43. struct page *page;
  44. struct file *file;
  45. u64 pgoff;
  46. } page_buf[PAGE_BUFS];
  47. struct mutex sender_lock;
  48. struct mutex receiver_lock;
  49. wait_queue_head_t send, recv;
  50. struct gipc_sender_buf {
  51. unsigned long bitmap[PAGE_BITS];
  52. struct page *pages[PAGE_BUFS];
  53. struct vm_area_struct *vmas[PAGE_BUFS];
  54. struct file *files[PAGE_BUFS];
  55. u64 pgoffs[PAGE_BUFS];
  56. } *sender_buf;
  57. };
  58. #define GIPC_PAGES(queue) \
  59. ((struct gipc_pages *) ((void *) (queue) + sizeof(struct gipc_queue)))
  60. struct {
  61. spinlock_t lock;
  62. /*
  63. * For now, just make them monotonically increasing. XXX: At
  64. * some point, do something smarter for security.
  65. */
  66. int64_t max_token;
  67. struct list_head channels; // gipc_queue structs
  68. } gdev;
  69. static inline struct gipc_queue * create_gipc_queue(struct file *creator)
  70. {
  71. struct gipc_queue *gq = kmem_cache_alloc(gipc_queue_cachep, GFP_KERNEL);
  72. struct gipc_pages *gp = GIPC_PAGES(gq);
  73. if (!gq)
  74. return gq;
  75. INIT_LIST_HEAD(&gq->list);
  76. memset(gp, 0, sizeof(struct gipc_pages) * 2);
  77. mutex_init(&gp[0].sender_lock);
  78. mutex_init(&gp[1].sender_lock);
  79. mutex_init(&gp[0].receiver_lock);
  80. mutex_init(&gp[1].receiver_lock);
  81. init_waitqueue_head(&gp[0].send);
  82. init_waitqueue_head(&gp[0].recv);
  83. init_waitqueue_head(&gp[1].send);
  84. init_waitqueue_head(&gp[1].recv);
  85. gp[0].file = creator;
  86. creator->private_data = gq;
  87. gp[1].file = NULL;
  88. spin_lock(&gdev.lock);
  89. list_add(&gq->list, &gdev.channels);
  90. gq->token = gdev.max_token++;
  91. #ifdef gipc_get_session
  92. gq->owner = gipc_get_session(current);
  93. #else
  94. gq->owner = my_gipc_get_session ? my_gipc_get_session(current) : 0;
  95. #endif
  96. spin_unlock(&gdev.lock);
  97. return gq;
  98. }
  99. static inline void release_gipc_pages(struct gipc_pages *gps) {
  100. int idx;
  101. while (gps->next != gps->last) {
  102. idx = gps->next;
  103. if (gps->page_buf[idx].page) {
  104. page_cache_release(gps->page_buf[idx].page);
  105. gps->page_buf[idx].page = NULL;
  106. }
  107. if (gps->page_buf[idx].file) {
  108. fput_atomic(gps->page_buf[idx].file);
  109. gps->page_buf[idx].file = NULL;
  110. gps->page_buf[idx].pgoff = 0;
  111. }
  112. gps->next++;
  113. gps->next &= (PAGE_BUFS-1);
  114. }
  115. }
  116. #if defined(SPLIT_RSS_COUNTING)
  117. static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
  118. {
  119. struct task_struct *task = current;
  120. if (likely(task->mm == mm))
  121. task->rss_stat.count[member] += val;
  122. else
  123. add_mm_counter(mm, member, val);
  124. }
  125. #else
  126. #define add_mm_counter_fast(mm, member, val) add_mm_counter(mm, member, val)
  127. #endif
  128. #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
  129. #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
  130. inline int make_pages_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  131. unsigned long addr, int count)
  132. {
  133. int i = 0;
  134. pgd_t *pgd;
  135. unsigned long pgd_next;
  136. pud_t *pud;
  137. unsigned long pud_next;
  138. pmd_t *pmd;
  139. unsigned long pmd_next;
  140. pte_t *pte;
  141. spinlock_t *ptl;
  142. unsigned long end = addr + (PAGE_SIZE * count);
  143. /* Mark source COW */
  144. do {
  145. if ((vma->vm_flags & (VM_SHARED|VM_MAYWRITE)) != VM_MAYWRITE)
  146. return VM_FAULT_OOM;
  147. pgd = pgd_offset(mm, addr);
  148. do {
  149. pgd_next = pgd_addr_end(addr, end);
  150. if (pgd_none(*pgd) || pgd_bad(*pgd))
  151. return VM_FAULT_OOM;
  152. pud = pud_offset(pgd, addr);
  153. do {
  154. pud_next = pud_addr_end(addr, pgd_next);
  155. if (pud_none(*pud) || pud_bad(*pud))
  156. return VM_FAULT_OOM;
  157. pmd = pmd_offset(pud, addr);
  158. do {
  159. pte_t *pte_base;
  160. pmd_next = pmd_addr_end(addr, pud_next);
  161. if (pmd_none(*pmd) || pmd_bad(*pmd))
  162. return VM_FAULT_OOM;
  163. pte_base = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  164. if (!pte)
  165. return VM_FAULT_OOM;
  166. do {
  167. if ((!pte_none(*pte)) && pte_present(*pte)) {
  168. ptep_set_wrprotect(mm, addr, pte);
  169. pte_wrprotect(*pte);
  170. } else {
  171. pte_unmap_unlock(pte, ptl);
  172. return VM_FAULT_OOM;
  173. }
  174. } while (pte++, addr += PAGE_SIZE, i++,
  175. addr != pmd_next);
  176. pte_unmap_unlock(pte_base, ptl);
  177. } while (pmd++,
  178. addr < pud_next);
  179. } while (pud++,
  180. addr < pgd_next);
  181. } while (pgd++,
  182. addr < vma->vm_end && addr < end);
  183. if (i < count) {
  184. /* Find the next vma. Leverage the fact that
  185. * addresses are increasing.
  186. */
  187. while (vma && addr < vma->vm_end)
  188. vma = vma->vm_next;
  189. }
  190. } while (addr < end && vma);
  191. return 0;
  192. }
  193. static void fill_page_bitmap(struct mm_struct *mm, unsigned long addr,
  194. unsigned long * page_bit_map, int count)
  195. {
  196. int i = 0;
  197. spinlock_t *ptl;
  198. pgd_t *pgd;
  199. unsigned long pgd_next;
  200. pud_t *pud;
  201. unsigned long pud_next;
  202. pmd_t *pmd;
  203. unsigned long pmd_next;
  204. pte_t *pte;
  205. unsigned long end = addr + (PAGE_SIZE * count);
  206. pgd = pgd_offset(mm, addr);
  207. do {
  208. pgd_next = pgd_addr_end(addr, end);
  209. if (pgd_none(*pgd) || pgd_bad(*pgd)) {
  210. while (addr < pgd_next) {
  211. clear_bit(i, page_bit_map);
  212. i++;
  213. addr += PAGE_SIZE;
  214. }
  215. continue;
  216. }
  217. pud = pud_offset(pgd, addr);
  218. do {
  219. pud_next = pud_addr_end(addr, pgd_next);
  220. if (pud_none(*pud) || pud_bad(*pud)) {
  221. while (addr < pud_next) {
  222. clear_bit(i, page_bit_map);
  223. i++;
  224. addr += PAGE_SIZE;
  225. }
  226. continue;
  227. }
  228. pmd = pmd_offset(pud, addr);
  229. do {
  230. pmd_next = pmd_addr_end(addr, pud_next);
  231. if(pmd_none(*pmd) || pmd_bad(*pmd)) {
  232. while (addr < pmd_next) {
  233. clear_bit(i, page_bit_map);
  234. i++;
  235. addr += PAGE_SIZE;
  236. }
  237. continue;
  238. }
  239. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  240. do {
  241. if ((!pte) || pte_none(*pte) || (!pte_present(*pte)))
  242. clear_bit(i, page_bit_map);
  243. else
  244. set_bit(i, page_bit_map);
  245. } while (pte++, addr += PAGE_SIZE, i++,
  246. addr != pmd_next);
  247. pte_unmap_unlock(pte - 1, ptl);
  248. } while (pmd++,
  249. addr < pud_next);
  250. } while (pud++,
  251. addr < pgd_next);
  252. } while (pgd++,
  253. addr < end);
  254. }
  255. static int get_pages (struct task_struct *current_tsk,
  256. unsigned long address, unsigned long * page_bit_map,
  257. struct page *pages[PAGE_BUFS],
  258. struct vm_area_struct *vmas[PAGE_BUFS], int count)
  259. {
  260. int num_contiguous_bits= 0;
  261. int index = 0;
  262. int i = 0, start, rv, page_count = 0;
  263. unsigned long addr = address;
  264. struct vm_area_struct *last_vma = NULL;
  265. while ( index < count ) {
  266. start = index;
  267. if ( test_bit(start, page_bit_map) ) {
  268. index = find_next_zero_bit(page_bit_map, PAGE_BUFS, start+1);
  269. if (index > count)
  270. index = count;
  271. num_contiguous_bits = index - start;
  272. rv = get_user_pages(current_tsk,
  273. current_tsk->mm,
  274. addr,
  275. num_contiguous_bits,
  276. 1,
  277. 1,
  278. &pages[start],
  279. &vmas[start]);
  280. if (rv <= 0) {
  281. printk(KERN_ERR "Graphene error: "
  282. "get_user_pages at 0x%016lx-0x%016lx\n",
  283. addr,
  284. addr + PAGE_SIZE * num_contiguous_bits);
  285. return rv;
  286. }
  287. if (rv != num_contiguous_bits) {
  288. printk(KERN_ERR "Graphene error: "
  289. "get_user_pages at 0x%016lx\n",
  290. addr + PAGE_SIZE * rv);
  291. return -EACCES;
  292. }
  293. page_count += rv;
  294. /* Mark source COW */
  295. rv = make_pages_cow(current_tsk->mm,
  296. vmas[start], addr, num_contiguous_bits);
  297. if (rv)
  298. return rv;
  299. /* Fix up the counters */
  300. add_mm_counter_fast(current_tsk->mm, MM_FILEPAGES, num_contiguous_bits);
  301. add_mm_counter_fast(current_tsk->mm, MM_ANONPAGES, -num_contiguous_bits);
  302. for (i = 0; i < num_contiguous_bits; i++) {
  303. pages[start + i]->mapping = NULL;
  304. addr += PAGE_SIZE;
  305. }
  306. last_vma = vmas[start + num_contiguous_bits - 1];
  307. } else {
  308. /* This is the case where a page (or pages) are not currently mapped.
  309. * Handle the hole appropriately.
  310. */
  311. index = find_next_bit(page_bit_map, PAGE_BUFS, start+1);
  312. if (index > count)
  313. index = count;
  314. num_contiguous_bits = index - start;
  315. for (i = 0; i < num_contiguous_bits; i++) {
  316. struct vm_area_struct *my_vma;
  317. pages[start + i] = NULL;
  318. if ( !last_vma ) {
  319. last_vma = find_vma(current_tsk->mm,
  320. addr);
  321. my_vma = last_vma;
  322. } else {
  323. /* DEP 6/17/13 - these addresses should be
  324. * monotonically increasing.
  325. */
  326. while (last_vma && addr >= last_vma->vm_end )
  327. last_vma = last_vma->vm_next;
  328. /* Leverage monotonic increasing vmas
  329. * to more quickly detect holes in the address space.
  330. */
  331. if (addr < last_vma->vm_start)
  332. my_vma = NULL;
  333. else
  334. my_vma = last_vma;
  335. }
  336. vmas[start + i] = my_vma;
  337. page_count++;
  338. addr += PAGE_SIZE;
  339. }
  340. }
  341. }
  342. return page_count;
  343. }
  344. static inline
  345. int recv_next (struct task_struct *current_tsk,
  346. struct gipc_pages *pending)
  347. {
  348. if (pending->next == pending->last) {
  349. /* The blocking condition for send
  350. * and recv can't both be true! */
  351. wake_up_all(&pending->send);
  352. wait_event_interruptible(pending->recv, (pending->next != pending->last));
  353. if (signal_pending(current_tsk))
  354. return -ERESTARTSYS;
  355. }
  356. return pending->next;
  357. }
  358. static
  359. int recv_helper (unsigned long *addr, unsigned long len, int prot,
  360. struct task_struct *current_tsk, struct gipc_pages *pending,
  361. struct file *file, int *physical_pages)
  362. {
  363. int page_count = len >> PAGE_SHIFT;
  364. int i, found;
  365. long rv = 0;
  366. struct page *page;
  367. struct file *vm_file;
  368. u64 vm_pgoff;
  369. int flags = MAP_PRIVATE;
  370. struct vm_area_struct *vma;
  371. unsigned long my_addr = *addr;
  372. if (len & ~PAGE_MASK)
  373. page_count++;
  374. for (i = 0; i < page_count; ) {
  375. unsigned long start_addr;
  376. found = recv_next(current_tsk, pending);
  377. if (found < 0)
  378. return -ERESTARTSYS;
  379. page = pending->page_buf[found].page;
  380. vm_file = pending->page_buf[found].file;
  381. vm_pgoff = pending->page_buf[found].pgoff;
  382. pending->next = ((pending->next + 1) & (PAGE_BUFS-1));
  383. wake_up_all(&pending->send);
  384. if (my_addr)
  385. flags |= MAP_FIXED;
  386. if (file)
  387. flags = (flags & ~MAP_ANONYMOUS) | MAP_FILE;
  388. else
  389. flags = (flags & ~MAP_FILE) | MAP_ANONYMOUS;
  390. /* Speculate that we will want the entire region under one
  391. * vma. Correct if not.
  392. */
  393. #if defined(CONFIG_GRAPHENE_BULK_IPC) || LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
  394. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)
  395. my_addr = do_mmap_pgoff(vm_file, my_addr,
  396. PAGE_SIZE * (page_count - i),
  397. prot, flags, vm_pgoff);
  398. #else
  399. {
  400. unsigned long populate;
  401. my_addr = do_mmap_pgoff(vm_file, my_addr,
  402. PAGE_SIZE * (page_count - i),
  403. prot,
  404. flags, vm_pgoff, &populate);
  405. }
  406. #endif /* kernel_version >= 3.9.0 */
  407. #else
  408. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)
  409. my_addr = my_do_mmap_pgoff(vm_file, my_addr,
  410. PAGE_SIZE * (page_count - i),
  411. prot, flags, vm_pgoff);
  412. #else
  413. {
  414. unsigned long populate;
  415. my_addr = my_do_mmap_pgoff(vm_file, my_addr,
  416. PAGE_SIZE * (page_count - i),
  417. prot,
  418. flags, vm_pgoff, &populate);
  419. }
  420. #endif /* kernel_version >= 3.9.0 */
  421. #endif
  422. if (!my_addr) {
  423. printk(KERN_ERR
  424. "Graphene error: failed to mmap %p\n",
  425. (void *) rv);
  426. rv = -EINVAL;
  427. goto finish_recv;
  428. }
  429. /* Save our staring addr for COW-ing later */
  430. *addr = start_addr = my_addr;
  431. vma = find_vma(current_tsk->mm, my_addr);
  432. if (!vma) {
  433. printk(KERN_ERR
  434. "Graphene error: can't find vma at %p\n",
  435. (void *) my_addr);
  436. rv = -ENOENT;
  437. goto finish_recv;
  438. }
  439. while (i < page_count) {
  440. int last_time = ((i+1) == page_count);
  441. int page_true = (page != NULL);
  442. /* Fill the vma with this page */
  443. if (page) {
  444. rv = vm_insert_page(vma, my_addr, page);
  445. if (rv) {
  446. printk(KERN_ERR
  447. "Graphene error: fail to insert page %p\n",
  448. (void *) rv);
  449. goto finish_recv;
  450. }
  451. (*physical_pages)++;
  452. page_cache_release(page);
  453. /* Avoid double-putting the page */
  454. page = NULL;
  455. }
  456. /* Check out the next page, maybe it can go in the same VMA */
  457. if (!last_time) {
  458. found = recv_next(current_tsk, pending);
  459. if (found < 0)
  460. return -ERESTARTSYS;
  461. }
  462. if (page_true) {
  463. /* If the next page will break the vma, isn't there,
  464. * or we are at the end of the VMA, go ahead
  465. * and mark the range we've mapped as COW.
  466. */
  467. if ((i+1) == page_count
  468. || (!pending->page_buf[found].page)
  469. || pending->page_buf[found].file != vm_file) {
  470. int sz = ((my_addr - start_addr) / PAGE_SIZE) + 1;
  471. rv = make_pages_cow(current_tsk->mm, vma,
  472. start_addr,
  473. sz);
  474. start_addr = my_addr + PAGE_SIZE;
  475. if (rv) {
  476. printk(KERN_ERR
  477. "Graphene error: can't make vma copy-on-write at %p-%p\n",
  478. (void *) start_addr,
  479. (void *) my_addr);
  480. goto finish_recv;
  481. }
  482. }
  483. }
  484. /* See if we can continue in the inner loop*/
  485. if ((!last_time) && pending->page_buf[found].file == vm_file) {
  486. page = pending->page_buf[found].page;
  487. pending->next = ((pending->next + 1) & (PAGE_BUFS-1));
  488. wake_up_all(&pending->send);
  489. i++;
  490. my_addr += PAGE_SIZE;
  491. /* Handle the case where we had a run of missing pages
  492. * and then one shows up
  493. */
  494. if (page && !page_true)
  495. start_addr = my_addr;
  496. } else
  497. break;
  498. }
  499. finish_recv:
  500. /* Drop the kernel's reference to this page */
  501. if (page)
  502. page_cache_release(page);
  503. if (vm_file)
  504. fput_atomic(vm_file);
  505. if (rv)
  506. return rv;
  507. i++;
  508. my_addr += PAGE_SIZE;
  509. }
  510. return page_count;
  511. }
  512. static long gipc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  513. {
  514. struct task_struct *current_tsk = current;
  515. long rv = 0;
  516. switch (cmd) {
  517. case GIPC_SEND: {
  518. unsigned long addrs[ADDR_ENTS], lens[ADDR_ENTS];
  519. unsigned long *page_bitmap;
  520. struct page **pages;
  521. struct vm_area_struct **vmas;
  522. struct file **files;
  523. u64 *pgoffs;
  524. struct gipc_send gs;
  525. unsigned long page_count, total_page_count;
  526. int page_copied = 0, physical_pages = 0;
  527. int i, j;
  528. struct gipc_queue *queue = NULL;
  529. struct gipc_pages *pending = NULL;
  530. rv = copy_from_user(&gs, (void *) arg, sizeof(gs));
  531. if (rv) {
  532. printk(KERN_ALERT "Graphene SEND: bad buffer %p\n",
  533. (void *) arg);
  534. return -EFAULT;
  535. }
  536. if (gs.entries > ADDR_ENTS) {
  537. printk(KERN_ALERT "Graphene SEND: too many entries\n");
  538. return -EINVAL;
  539. }
  540. rv = copy_from_user(&addrs, gs.addr,
  541. sizeof(unsigned long) * gs.entries);
  542. if (rv) {
  543. printk(KERN_ALERT "Graphene SEND: bad buffer %p\n",
  544. gs.addr);
  545. return -EFAULT;
  546. }
  547. rv = copy_from_user(&lens, gs.len,
  548. sizeof(unsigned long) * gs.entries);
  549. if (rv) {
  550. printk(KERN_ALERT "Graphene SEND: bad buffer %p\n",
  551. gs.len);
  552. return -EFAULT;
  553. }
  554. for (j = 0; j < gs.entries; j++) {
  555. unsigned long addr = addrs[j]; //user pointers
  556. unsigned long len = lens[j]; //user supplied lens
  557. if (addr > addr + len) {
  558. printk(KERN_ALERT "Graphene SEND:"
  559. " attempt to send %p - %p "
  560. " by thread %d FAIL: bad length\n",
  561. (void *) addr, (void *) (addr + len),
  562. current_tsk->pid);
  563. return -EINVAL;
  564. }
  565. }
  566. /* Find/allocate the gipc_pages struct for our recipient */
  567. queue = (struct gipc_queue *) file->private_data;
  568. if (!queue)
  569. return -EFAULT;
  570. /* We want to put pages in the partner buf */
  571. if (file == GIPC_PAGES(queue)[0].file) {
  572. pending = &GIPC_PAGES(queue)[1];
  573. } else {
  574. pending = &GIPC_PAGES(queue)[0];
  575. BUG_ON(pending->file != file);
  576. }
  577. DEBUG("GIPC_SEND %ld entries to token %lld by thread %d\n",
  578. gs.entries, queue->token, current_tsk->pid);
  579. for (j = 0; j < gs.entries; j++) {
  580. unsigned long addr = addrs[j]; //user pointers
  581. unsigned long len = lens[j]; //user supplied lens
  582. int get_page_failed = 0;
  583. total_page_count = len >> PAGE_SHIFT;
  584. if (len & ~PAGE_MASK)
  585. total_page_count++;
  586. if (!access_ok(VERIFY_READ, addr,
  587. total_page_count << PAGE_SHIFT)) {
  588. printk(KERN_ALERT "Graphene SEND:"
  589. " attempt to send %p - %p (%ld pages) "
  590. " by thread %d FAIL: bad permission\n",
  591. (void *) addr, (void *) (addr + len),
  592. total_page_count, current_tsk->pid);
  593. return -EFAULT;
  594. }
  595. DEBUG(" %p - %p (%ld pages) sent by thread %d\n",
  596. (void *) addr, (void *) (addr + len),
  597. len >> PAGE_SHIFT, current_tsk->pid);
  598. while (total_page_count) {
  599. page_count = total_page_count <= PAGE_BUFS
  600. ? total_page_count : PAGE_BUFS;
  601. total_page_count -= page_count;
  602. /* for each of these addresses - check if
  603. * demand faulting will be triggered
  604. * if vma is present, but there is no page
  605. * present(pmd/pud not present or PTE_PRESENT
  606. * is off) then get_user_pages will trigger
  607. * the creation of those */
  608. mutex_lock(&pending->sender_lock);
  609. if (!pending->sender_buf)
  610. pending->sender_buf =
  611. kmem_cache_alloc(gipc_buf_cachep,
  612. GFP_KERNEL);
  613. page_bitmap = pending->sender_buf->bitmap;
  614. pages = pending->sender_buf->pages;
  615. vmas = pending->sender_buf->vmas;
  616. files = pending->sender_buf->files;
  617. pgoffs = pending->sender_buf->pgoffs;
  618. down_write(&current_tsk->mm->mmap_sem);
  619. fill_page_bitmap(current_tsk->mm,
  620. addr,
  621. page_bitmap,
  622. page_count);
  623. rv = get_pages(current_tsk,
  624. addr,
  625. page_bitmap,
  626. pages,
  627. vmas,
  628. page_count);
  629. if (rv < 0) {
  630. up_write(&current_tsk->mm->mmap_sem);
  631. mutex_unlock(&pending->sender_lock);
  632. goto out;
  633. }
  634. for (i = 0; i < page_count; i++) {
  635. BUG_ON((!vmas[i]) && (pages[i]));
  636. if (vmas[i]) {
  637. files[i] = vmas[i]->vm_file;
  638. if (files[i]) {
  639. get_file(files[i]);
  640. pgoffs[i] = ((addr - vmas[i]->vm_start) >> PAGE_SHIFT)
  641. + vmas[i]->vm_pgoff;
  642. } else {
  643. pgoffs[i] = 0;
  644. }
  645. } else {
  646. files[i] = NULL;
  647. pgoffs[i] = 0;
  648. }
  649. if (pages[i])
  650. physical_pages++;
  651. addr += PAGE_SIZE;
  652. }
  653. up_write(&current_tsk->mm->mmap_sem);
  654. for (i = 0; i < page_count ; i++) {
  655. /* Put in the pending buffer*/
  656. if ( ((pending->last + 1)
  657. & (PAGE_BUFS-1) )
  658. == pending->next) {
  659. /* The blocking condition for send
  660. * and recv can't both be true! */
  661. wake_up_all(&pending->recv);
  662. wait_event_interruptible(pending->send,
  663. ( ((pending->last + 1)
  664. & (PAGE_BUFS-1) )
  665. != pending->next));
  666. if (signal_pending(current_tsk)) {
  667. mutex_unlock(&pending->sender_lock);
  668. return -ERESTARTSYS;
  669. }
  670. }
  671. pending->page_buf[pending->last].page = pages[i];
  672. pending->page_buf[pending->last].file = files[i];
  673. pending->page_buf[pending->last].pgoff = pgoffs[i];
  674. pending->last = ((pending->last + 1) & (PAGE_BUFS-1));
  675. }
  676. mutex_unlock(&pending->sender_lock);
  677. wake_up_all(&pending->recv);
  678. page_copied += page_count;
  679. if (get_page_failed)
  680. goto out;
  681. }
  682. if (get_page_failed)
  683. goto out;
  684. }
  685. out:
  686. DEBUG("GIPC_SEND return to thread %d, %d pages "
  687. ` "(%d physical pages) are sent\n",
  688. current_tsk->pid, page_copied, physical_pages);
  689. rv = page_copied;
  690. break;
  691. }
  692. case GIPC_RECV:
  693. {
  694. unsigned long addrs[ADDR_ENTS], lens[ADDR_ENTS];
  695. int prots[ADDR_ENTS];
  696. struct gipc_recv gr;
  697. int page_copied = 0;
  698. int j;
  699. struct gipc_pages *pending = NULL;
  700. struct gipc_queue *queue = (struct gipc_queue *) file->private_data;
  701. int physical_pages = 0;
  702. if (!queue)
  703. return -EFAULT;
  704. if (file == GIPC_PAGES(queue)[0].file)
  705. pending = &GIPC_PAGES(queue)[0];
  706. else {
  707. pending = &GIPC_PAGES(queue)[1];
  708. BUG_ON(pending->file != file);
  709. }
  710. rv = copy_from_user(&gr, (void *) arg, sizeof(gr));
  711. if (rv) {
  712. printk(KERN_ERR "Graphene error: bad buffer %p\n",
  713. (void *) arg);
  714. return -EFAULT;
  715. }
  716. if (gr.entries > ADDR_ENTS) {
  717. printk(KERN_ALERT "Graphene RECV: too many entries\n");
  718. return -EINVAL;
  719. }
  720. rv = copy_from_user(&addrs, gr.addr,
  721. sizeof(unsigned long) * gr.entries);
  722. if (rv) {
  723. printk(KERN_ALERT "Graphene RECV: bad buffer %p\n",
  724. gr.addr);
  725. return -EFAULT;
  726. }
  727. rv = copy_from_user(&lens, gr.len,
  728. sizeof(unsigned long) * gr.entries);
  729. if (rv) {
  730. printk(KERN_ALERT "Graphene RECV: bad buffer %p\n",
  731. gr.len);
  732. return -EFAULT;
  733. }
  734. rv = copy_from_user(&prots, gr.prot,
  735. sizeof(int) * gr.entries);
  736. if (rv) {
  737. printk(KERN_ALERT "Graphene RECV: bad buffer %p\n",
  738. gr.prot);
  739. return -EFAULT;
  740. }
  741. down_write(&current_tsk->mm->mmap_sem);
  742. mutex_lock(&pending->receiver_lock);
  743. DEBUG("GIPC_RECV %ld entries to token %lld by thread %d\n",
  744. gr.entries, queue->token, current_tsk->pid);
  745. for (j = 0; j < gr.entries; j++) {
  746. rv = recv_helper(&addrs[j], lens[j], prots[j],
  747. current_tsk, pending, file,
  748. &physical_pages);
  749. if (rv < 0) {
  750. mutex_unlock(&pending->receiver_lock);
  751. up_write(&current_tsk->mm->mmap_sem);
  752. return rv;
  753. }
  754. DEBUG(" %p - %p (%ld pages) received by thread %d\n",
  755. (void *) addrs[j],
  756. (void *) addrs[j] + (rv << PAGE_SHIFT),
  757. rv, current_tsk->pid);
  758. page_copied += rv;
  759. }
  760. mutex_unlock(&pending->receiver_lock);
  761. up_write(&current_tsk->mm->mmap_sem);
  762. rv = copy_to_user(((struct gipc_recv *)arg)->addr, addrs,
  763. sizeof(unsigned long) * gr.entries);
  764. if (rv) {
  765. printk(KERN_ERR "Graphene error: bad buffer %p\n",
  766. (void *) arg);
  767. return -EFAULT;
  768. }
  769. DEBUG("GIPC_RECV return to thread %d, %d pages "
  770. "(%d physical pages) are received\n",
  771. current_tsk->pid, page_copied, physical_pages);
  772. rv = page_copied;
  773. break;
  774. }
  775. case GIPC_CREATE:
  776. {
  777. struct gipc_queue *gq = create_gipc_queue(file);
  778. if (gq == NULL)
  779. return -ENOMEM;
  780. DEBUG("GIPC_CREATE token %lld by thread %d\n", gq->token,
  781. current_tsk->pid);
  782. rv = gq->token;
  783. break;
  784. }
  785. case GIPC_JOIN:
  786. {
  787. struct gipc_queue *gq;
  788. s64 token = arg;
  789. #ifdef gipc_get_session
  790. u32 session = gipc_get_session(current);
  791. #else
  792. u32 session = my_gipc_get_session ? my_gipc_get_session(current_tsk) : 0;
  793. #endif
  794. if (file->private_data != NULL)
  795. return -EBUSY;
  796. /* Search for this token */
  797. spin_lock(&gdev.lock);
  798. list_for_each_entry(gq, &gdev.channels, list) {
  799. if (gq->token == token)
  800. break;
  801. }
  802. /* Fail if we didn't find it */
  803. if (gq == NULL || &gq->list == &gdev.channels) {
  804. spin_unlock(&gdev.lock);
  805. return -ENOENT;
  806. }
  807. if (gq->owner != session) {
  808. spin_unlock(&gdev.lock);
  809. return -EPERM;
  810. }
  811. if (GIPC_PAGES(gq)[1].file == NULL) {
  812. GIPC_PAGES(gq)[1].file = file;
  813. file->private_data = gq;
  814. } else {
  815. spin_unlock(&gdev.lock);
  816. return -EBUSY;
  817. }
  818. /* Hold the lock until we allocate so only one process
  819. * gets the queue */
  820. spin_unlock(&gdev.lock);
  821. DEBUG("GIPC_JOIN token %lld by thread %d\n", token,
  822. current_tsk->pid);
  823. rv = 0;
  824. break;
  825. }
  826. default:
  827. printk(KERN_ALERT "Graphene unknown ioctl %u %lu\n", cmd, arg);
  828. rv = -ENOSYS;
  829. break;
  830. }
  831. return rv;
  832. }
  833. static int gipc_release(struct inode *inode, struct file *file)
  834. {
  835. int free = 0;
  836. struct gipc_queue *gq = (struct gipc_queue *) file->private_data;
  837. struct gipc_pages *gp = GIPC_PAGES(gq);
  838. if (gq) {
  839. struct gipc_pages *local, *other;
  840. spin_lock(&gdev.lock);
  841. if (gp[0].file == file) {
  842. local = &gp[0];
  843. other = &gp[1];
  844. } else {
  845. local = &gp[1];
  846. other = &gp[0];
  847. BUG_ON(local->file != file);
  848. }
  849. release_gipc_pages(local);
  850. /* Detect whether we are the last one out by poisoning the file field */
  851. local->file = FILE_POISON;
  852. if (other->file == FILE_POISON) {
  853. free = 1;
  854. list_del(&gq->list);
  855. }
  856. spin_unlock(&gdev.lock);
  857. }
  858. if (free) {
  859. if (gp[0].sender_buf)
  860. kmem_cache_free(gipc_buf_cachep, gp[0].sender_buf);
  861. if (gp[1].sender_buf)
  862. kmem_cache_free(gipc_buf_cachep, gp[1].sender_buf);
  863. kmem_cache_free(gipc_queue_cachep, gq);
  864. }
  865. file->private_data = NULL;
  866. return 0;
  867. }
  868. static int gipc_open(struct inode *inode, struct file *file)
  869. {
  870. file->private_data = NULL;
  871. return 0;
  872. }
  873. static struct file_operations gipc_fops = {
  874. .owner = THIS_MODULE,
  875. .release = gipc_release,
  876. .open = gipc_open,
  877. .unlocked_ioctl = gipc_ioctl,
  878. .compat_ioctl = gipc_ioctl,
  879. .llseek = noop_llseek,
  880. };
  881. static struct miscdevice gipc_dev = {
  882. .minor = GIPC_MINOR,
  883. .name = "gipc",
  884. .fops = &gipc_fops,
  885. .mode = 0666,
  886. };
  887. static int __init gipc_init(void)
  888. {
  889. int rv = 0;
  890. #if !defined(CONFIG_GRAPHENE_BULK_IPC) && LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
  891. my_do_mmap_pgoff = (do_mmap_pgoff_t)
  892. kallsyms_lookup_name("do_mmap_pgoff");
  893. printk(KERN_ERR "resolved symbol do_mmap_pgoff %p\n", my_do_mmap_pgoff);
  894. if (!my_do_mmap_pgoff) {
  895. printk(KERN_ERR "Graphene error: "
  896. "can't find kernel function do_mmap_pgoff\n");
  897. return -ENOENT;
  898. }
  899. #endif
  900. #ifndef gipc_get_session
  901. my_gipc_get_session = (void *) kallsyms_lookup_name("gipc_get_session");
  902. #endif
  903. #if 0 /* these functions are no longer used, keep here for future use. */
  904. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
  905. my_tlb_gather_mmu = (tlb_gather_mmu_t)
  906. kallsyms_lookup_name("tlb_gather_mmu");
  907. printk(KERN_ERR "resolved symbol tlb_gather_mmu %p\n", my_tlb_gather_mmu);
  908. if (!my_tlb_gather_mmu) {
  909. printk(KERN_ERR "Graphene error: "
  910. "can't find kernel function my_tlb_gather_mmu\n");
  911. return -ENOENT;
  912. }
  913. my_tlb_flush_mmu = (tlb_flush_mmu_t)
  914. kallsyms_lookup_name("tlb_flush_mmu");
  915. if (!my_tlb_flush_mmu) {
  916. printk(KERN_ERR "Graphene error: "
  917. "can't find kernel function my_tlb_flush_mmu\n");
  918. return -ENOENT;
  919. }
  920. my_tlb_finish_mmu = (tlb_finish_mmu_t)
  921. kallsyms_lookup_name("tlb_finish_mmu");
  922. if (!my_tlb_finish_mmu) {
  923. printk(KERN_ERR "Graphene error: "
  924. "can't find kernel function my_tlb_finish_mmu\n");
  925. return -ENOENT;
  926. }
  927. #else
  928. pmmu_gathers = (struct mmu_gather *)
  929. kallsyms_lookup_name("mmu_gathers");
  930. if (!pmmu_gathers) {
  931. printk(KERN_ERR "Graphene error: "
  932. "can't find kernel function mmu_gathers\n");
  933. return -ENOENT;
  934. }
  935. #endif /* kernel_version < 3.2 */
  936. kern_free_pages_and_swap_cachep = (free_pages_and_swap_cache_t)
  937. kallsyms_lookup_name("free_pages_and_swap_cache");
  938. if (!kern_free_pages_and_swap_cachep) {
  939. printk(KERN_ERR "Graphene error: "
  940. "can't find kernel function free_pages_and_swap_cache\n");
  941. return -ENOENT;
  942. }
  943. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)
  944. kern_flush_tlb_mm = (flush_tlb_mm_t)
  945. kallsyms_lookup_name("flush_tlb_mm");
  946. if (!kern_flush_tlb_mm) {
  947. printk(KERN_ERR "Graphene error: "
  948. "can't find kernel function flush_tlb_mm\n");
  949. return -ENOENT;
  950. }
  951. #endif
  952. kern_free_pgtables = (free_pgtables_t)
  953. kallsyms_lookup_name("free_pgtables");
  954. if (!kern_free_pgtables) {
  955. printk(KERN_ERR "Graphene error: "
  956. "can't find kernel function free_pgtables\n");
  957. return -ENOENT;
  958. }
  959. #endif
  960. /* Register the kmem cache */
  961. gipc_queue_cachep = kmem_cache_create("gipc_queues",
  962. sizeof(struct gipc_queue) +
  963. sizeof(struct gipc_pages) * 2,
  964. 0,
  965. SLAB_HWCACHE_ALIGN|
  966. SLAB_DESTROY_BY_RCU,
  967. NULL);
  968. if (!gipc_queue_cachep) {
  969. printk(KERN_ERR "Graphene error: "
  970. "failed to create a gipc queues cache\n");
  971. return -ENOMEM;
  972. }
  973. gipc_buf_cachep = kmem_cache_create("gipc_bufs",
  974. sizeof(struct gipc_sender_buf),
  975. 0,
  976. SLAB_HWCACHE_ALIGN|
  977. SLAB_DESTROY_BY_RCU,
  978. NULL);
  979. if (!gipc_buf_cachep) {
  980. printk(KERN_ERR "Graphene error: "
  981. "failed to create a gipc buffers cache\n");
  982. return -ENOMEM;
  983. }
  984. INIT_LIST_HEAD(&gdev.channels);
  985. spin_lock_init(&gdev.lock);
  986. gdev.max_token = 1;
  987. rv = misc_register(&gipc_dev);
  988. if (rv) {
  989. printk(KERN_ERR "Graphene error: "
  990. "failed to add a char device (rv=%d)\n", rv);
  991. return rv;
  992. }
  993. printk(KERN_ALERT "Graphene IPC: Hello, world\n");
  994. return 0;
  995. }
  996. static void __exit gipc_exit(void)
  997. {
  998. struct gipc_queue *gq, *n;
  999. spin_lock(&gdev.lock);
  1000. list_for_each_entry_safe(gq, n, &gdev.channels, list) {
  1001. release_gipc_pages(&GIPC_PAGES(gq)[0]);
  1002. release_gipc_pages(&GIPC_PAGES(gq)[1]);
  1003. list_del(&gq->list);
  1004. kmem_cache_free(gipc_queue_cachep, gq);
  1005. }
  1006. spin_unlock(&gdev.lock);
  1007. misc_deregister(&gipc_dev);
  1008. kmem_cache_destroy(gipc_queue_cachep);
  1009. printk(KERN_ALERT "Graphene IPC: Goodbye, cruel world\n");
  1010. }
  1011. module_init(gipc_init);
  1012. module_exit(gipc_exit);