graphene-ipc.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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. my_addr = do_mmap_pgoff(vm_file, my_addr, PAGE_SIZE * (page_count - i),
  395. prot,
  396. flags, vm_pgoff);
  397. #elif LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)
  398. my_addr = my_do_mmap_pgoff(vm_file, my_addr, PAGE_SIZE * (page_count - i),
  399. prot,
  400. flags, vm_pgoff);
  401. #else
  402. {
  403. unsigned long populate;
  404. my_addr = my_do_mmap_pgoff(vm_file, my_addr, PAGE_SIZE * (page_count - i),
  405. prot,
  406. flags, vm_pgoff, &populate);
  407. }
  408. #endif /* kernel_version >= 3.9.0 */
  409. if (!my_addr) {
  410. printk(KERN_ERR
  411. "Graphene error: failed to mmap %p\n",
  412. (void *) rv);
  413. rv = -EINVAL;
  414. goto finish_recv;
  415. }
  416. /* Save our staring addr for COW-ing later */
  417. *addr = start_addr = my_addr;
  418. vma = find_vma(current_tsk->mm, my_addr);
  419. if (!vma) {
  420. printk(KERN_ERR
  421. "Graphene error: can't find vma at %p\n",
  422. (void *) my_addr);
  423. rv = -ENOENT;
  424. goto finish_recv;
  425. }
  426. while (i < page_count) {
  427. int last_time = ((i+1) == page_count);
  428. int page_true = (page != NULL);
  429. /* Fill the vma with this page */
  430. if (page) {
  431. rv = vm_insert_page(vma, my_addr, page);
  432. if (rv) {
  433. printk(KERN_ERR
  434. "Graphene error: fail to insert page %p\n",
  435. (void *) rv);
  436. goto finish_recv;
  437. }
  438. (*physical_pages)++;
  439. page_cache_release(page);
  440. /* Avoid double-putting the page */
  441. page = NULL;
  442. }
  443. /* Check out the next page, maybe it can go in the same VMA */
  444. if (!last_time) {
  445. found = recv_next(current_tsk, pending);
  446. if (found < 0)
  447. return -ERESTARTSYS;
  448. }
  449. if (page_true) {
  450. /* If the next page will break the vma, isn't there,
  451. * or we are at the end of the VMA, go ahead
  452. * and mark the range we've mapped as COW.
  453. */
  454. if ((i+1) == page_count
  455. || (!pending->page_buf[found].page)
  456. || pending->page_buf[found].file != vm_file) {
  457. int sz = ((my_addr - start_addr) / PAGE_SIZE) + 1;
  458. rv = make_pages_cow(current_tsk->mm, vma,
  459. start_addr,
  460. sz);
  461. start_addr = my_addr + PAGE_SIZE;
  462. if (rv) {
  463. printk(KERN_ERR
  464. "Graphene error: can't make vma copy-on-write at %p-%p\n",
  465. (void *) start_addr,
  466. (void *) my_addr);
  467. goto finish_recv;
  468. }
  469. }
  470. }
  471. /* See if we can continue in the inner loop*/
  472. if ((!last_time) && pending->page_buf[found].file == vm_file) {
  473. page = pending->page_buf[found].page;
  474. pending->next = ((pending->next + 1) & (PAGE_BUFS-1));
  475. wake_up_all(&pending->send);
  476. i++;
  477. my_addr += PAGE_SIZE;
  478. /* Handle the case where we had a run of missing pages
  479. * and then one shows up
  480. */
  481. if (page && !page_true)
  482. start_addr = my_addr;
  483. } else
  484. break;
  485. }
  486. finish_recv:
  487. /* Drop the kernel's reference to this page */
  488. if (page)
  489. page_cache_release(page);
  490. if (vm_file)
  491. fput_atomic(vm_file);
  492. if (rv)
  493. return rv;
  494. i++;
  495. my_addr += PAGE_SIZE;
  496. }
  497. return page_count;
  498. }
  499. static long gipc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  500. {
  501. struct task_struct *current_tsk = current;
  502. long rv = 0;
  503. switch (cmd) {
  504. case GIPC_SEND: {
  505. unsigned long addrs[ADDR_ENTS], lens[ADDR_ENTS];
  506. unsigned long *page_bitmap;
  507. struct page **pages;
  508. struct vm_area_struct **vmas;
  509. struct file **files;
  510. u64 *pgoffs;
  511. struct gipc_send gs;
  512. unsigned long page_count, total_page_count;
  513. int page_copied = 0, physical_pages = 0;
  514. int i, j;
  515. struct gipc_queue *queue = NULL;
  516. struct gipc_pages *pending = NULL;
  517. rv = copy_from_user(&gs, (void *) arg, sizeof(gs));
  518. if (rv) {
  519. printk(KERN_ALERT "Graphene SEND: bad buffer %p\n",
  520. (void *) arg);
  521. return -EFAULT;
  522. }
  523. if (gs.entries > ADDR_ENTS) {
  524. printk(KERN_ALERT "Graphene SEND: too many entries\n");
  525. return -EINVAL;
  526. }
  527. rv = copy_from_user(&addrs, gs.addr,
  528. sizeof(unsigned long) * gs.entries);
  529. if (rv) {
  530. printk(KERN_ALERT "Graphene SEND: bad buffer %p\n",
  531. gs.addr);
  532. return -EFAULT;
  533. }
  534. rv = copy_from_user(&lens, gs.len,
  535. sizeof(unsigned long) * gs.entries);
  536. if (rv) {
  537. printk(KERN_ALERT "Graphene SEND: bad buffer %p\n",
  538. gs.len);
  539. return -EFAULT;
  540. }
  541. for (j = 0; j < gs.entries; j++) {
  542. unsigned long addr = addrs[j]; //user pointers
  543. unsigned long len = lens[j]; //user supplied lens
  544. if (addr > addr + len) {
  545. printk(KERN_ALERT "Graphene SEND:"
  546. " attempt to send %p - %p "
  547. " by thread %d FAIL: bad length\n",
  548. (void *) addr, (void *) (addr + len),
  549. current_tsk->pid);
  550. return -EINVAL;
  551. }
  552. }
  553. /* Find/allocate the gipc_pages struct for our recipient */
  554. queue = (struct gipc_queue *) file->private_data;
  555. if (!queue)
  556. return -EFAULT;
  557. /* We want to put pages in the partner buf */
  558. if (file == GIPC_PAGES(queue)[0].file) {
  559. pending = &GIPC_PAGES(queue)[1];
  560. } else {
  561. pending = &GIPC_PAGES(queue)[0];
  562. BUG_ON(pending->file != file);
  563. }
  564. DEBUG("GIPC_SEND %ld entries to token %lld by thread %d\n",
  565. gs.entries, queue->token, current_tsk->pid);
  566. for (j = 0; j < gs.entries; j++) {
  567. unsigned long addr = addrs[j]; //user pointers
  568. unsigned long len = lens[j]; //user supplied lens
  569. int get_page_failed = 0;
  570. total_page_count = len >> PAGE_SHIFT;
  571. if (len & ~PAGE_MASK)
  572. total_page_count++;
  573. if (!access_ok(VERIFY_READ, addr,
  574. total_page_count << PAGE_SHIFT)) {
  575. printk(KERN_ALERT "Graphene SEND:"
  576. " attempt to send %p - %p (%ld pages) "
  577. " by thread %d FAIL: bad permission\n",
  578. (void *) addr, (void *) (addr + len),
  579. total_page_count, current_tsk->pid);
  580. return -EFAULT;
  581. }
  582. DEBUG(" %p - %p (%ld pages) sent by thread %d\n",
  583. (void *) addr, (void *) (addr + len),
  584. len >> PAGE_SHIFT, current_tsk->pid);
  585. while (total_page_count) {
  586. page_count = total_page_count <= PAGE_BUFS
  587. ? total_page_count : PAGE_BUFS;
  588. total_page_count -= page_count;
  589. /* for each of these addresses - check if
  590. * demand faulting will be triggered
  591. * if vma is present, but there is no page
  592. * present(pmd/pud not present or PTE_PRESENT
  593. * is off) then get_user_pages will trigger
  594. * the creation of those */
  595. mutex_lock(&pending->sender_lock);
  596. if (!pending->sender_buf)
  597. pending->sender_buf =
  598. kmem_cache_alloc(gipc_buf_cachep,
  599. GFP_KERNEL);
  600. page_bitmap = pending->sender_buf->bitmap;
  601. pages = pending->sender_buf->pages;
  602. vmas = pending->sender_buf->vmas;
  603. files = pending->sender_buf->files;
  604. pgoffs = pending->sender_buf->pgoffs;
  605. down_write(&current_tsk->mm->mmap_sem);
  606. fill_page_bitmap(current_tsk->mm,
  607. addr,
  608. page_bitmap,
  609. page_count);
  610. rv = get_pages(current_tsk,
  611. addr,
  612. page_bitmap,
  613. pages,
  614. vmas,
  615. page_count);
  616. if (rv < 0) {
  617. up_write(&current_tsk->mm->mmap_sem);
  618. mutex_unlock(&pending->sender_lock);
  619. goto out;
  620. }
  621. for (i = 0; i < page_count; i++) {
  622. BUG_ON((!vmas[i]) && (pages[i]));
  623. if (vmas[i]) {
  624. files[i] = vmas[i]->vm_file;
  625. if (files[i]) {
  626. get_file(files[i]);
  627. pgoffs[i] = ((addr - vmas[i]->vm_start) >> PAGE_SHIFT)
  628. + vmas[i]->vm_pgoff;
  629. } else {
  630. pgoffs[i] = 0;
  631. }
  632. } else {
  633. files[i] = NULL;
  634. pgoffs[i] = 0;
  635. }
  636. if (pages[i])
  637. physical_pages++;
  638. addr += PAGE_SIZE;
  639. }
  640. up_write(&current_tsk->mm->mmap_sem);
  641. for (i = 0; i < page_count ; i++) {
  642. /* Put in the pending buffer*/
  643. if ( ((pending->last + 1)
  644. & (PAGE_BUFS-1) )
  645. == pending->next) {
  646. /* The blocking condition for send
  647. * and recv can't both be true! */
  648. wake_up_all(&pending->recv);
  649. wait_event_interruptible(pending->send,
  650. ( ((pending->last + 1)
  651. & (PAGE_BUFS-1) )
  652. != pending->next));
  653. if (signal_pending(current_tsk)) {
  654. mutex_unlock(&pending->sender_lock);
  655. return -ERESTARTSYS;
  656. }
  657. }
  658. pending->page_buf[pending->last].page = pages[i];
  659. pending->page_buf[pending->last].file = files[i];
  660. pending->page_buf[pending->last].pgoff = pgoffs[i];
  661. pending->last = ((pending->last + 1) & (PAGE_BUFS-1));
  662. }
  663. mutex_unlock(&pending->sender_lock);
  664. wake_up_all(&pending->recv);
  665. page_copied += page_count;
  666. if (get_page_failed)
  667. goto out;
  668. }
  669. if (get_page_failed)
  670. goto out;
  671. }
  672. out:
  673. DEBUG("GIPC_SEND return to thread %d, %d pages "
  674. ` "(%d physical pages) are sent\n",
  675. current_tsk->pid, page_copied, physical_pages);
  676. rv = page_copied;
  677. break;
  678. }
  679. case GIPC_RECV:
  680. {
  681. unsigned long addrs[ADDR_ENTS], lens[ADDR_ENTS];
  682. int prots[ADDR_ENTS];
  683. struct gipc_recv gr;
  684. int page_copied = 0;
  685. int j;
  686. struct gipc_pages *pending = NULL;
  687. struct gipc_queue *queue = (struct gipc_queue *) file->private_data;
  688. int physical_pages = 0;
  689. if (!queue)
  690. return -EFAULT;
  691. if (file == GIPC_PAGES(queue)[0].file)
  692. pending = &GIPC_PAGES(queue)[0];
  693. else {
  694. pending = &GIPC_PAGES(queue)[1];
  695. BUG_ON(pending->file != file);
  696. }
  697. rv = copy_from_user(&gr, (void *) arg, sizeof(gr));
  698. if (rv) {
  699. printk(KERN_ERR "Graphene error: bad buffer %p\n",
  700. (void *) arg);
  701. return -EFAULT;
  702. }
  703. if (gr.entries > ADDR_ENTS) {
  704. printk(KERN_ALERT "Graphene RECV: too many entries\n");
  705. return -EINVAL;
  706. }
  707. rv = copy_from_user(&addrs, gr.addr,
  708. sizeof(unsigned long) * gr.entries);
  709. if (rv) {
  710. printk(KERN_ALERT "Graphene RECV: bad buffer %p\n",
  711. gr.addr);
  712. return -EFAULT;
  713. }
  714. rv = copy_from_user(&lens, gr.len,
  715. sizeof(unsigned long) * gr.entries);
  716. if (rv) {
  717. printk(KERN_ALERT "Graphene RECV: bad buffer %p\n",
  718. gr.len);
  719. return -EFAULT;
  720. }
  721. rv = copy_from_user(&prots, gr.prot,
  722. sizeof(int) * gr.entries);
  723. if (rv) {
  724. printk(KERN_ALERT "Graphene RECV: bad buffer %p\n",
  725. gr.prot);
  726. return -EFAULT;
  727. }
  728. down_write(&current_tsk->mm->mmap_sem);
  729. mutex_lock(&pending->receiver_lock);
  730. DEBUG("GIPC_RECV %ld entries to token %lld by thread %d\n",
  731. gr.entries, queue->token, current_tsk->pid);
  732. for (j = 0; j < gr.entries; j++) {
  733. rv = recv_helper(&addrs[j], lens[j], prots[j],
  734. current_tsk, pending, file,
  735. &physical_pages);
  736. if (rv < 0) {
  737. mutex_unlock(&pending->receiver_lock);
  738. up_write(&current_tsk->mm->mmap_sem);
  739. return rv;
  740. }
  741. DEBUG(" %p - %p (%ld pages) received by thread %d\n",
  742. (void *) addrs[j],
  743. (void *) addrs[j] + (rv << PAGE_SHIFT),
  744. rv, current_tsk->pid);
  745. page_copied += rv;
  746. }
  747. mutex_unlock(&pending->receiver_lock);
  748. up_write(&current_tsk->mm->mmap_sem);
  749. rv = copy_to_user(((struct gipc_recv *)arg)->addr, addrs,
  750. sizeof(unsigned long) * gr.entries);
  751. if (rv) {
  752. printk(KERN_ERR "Graphene error: bad buffer %p\n",
  753. (void *) arg);
  754. return -EFAULT;
  755. }
  756. DEBUG("GIPC_RECV return to thread %d, %d pages "
  757. "(%d physical pages) are received\n",
  758. current_tsk->pid, page_copied, physical_pages);
  759. rv = page_copied;
  760. break;
  761. }
  762. case GIPC_CREATE:
  763. {
  764. struct gipc_queue *gq = create_gipc_queue(file);
  765. if (gq == NULL)
  766. return -ENOMEM;
  767. DEBUG("GIPC_CREATE token %lld by thread %d\n", gq->token,
  768. current_tsk->pid);
  769. rv = gq->token;
  770. break;
  771. }
  772. case GIPC_JOIN:
  773. {
  774. struct gipc_queue *gq;
  775. s64 token = arg;
  776. #ifdef gipc_get_session
  777. u32 session = gipc_get_session(current);
  778. #else
  779. u32 session = my_gipc_get_session ? my_gipc_get_session(current_tsk) : 0;
  780. #endif
  781. if (file->private_data != NULL)
  782. return -EBUSY;
  783. /* Search for this token */
  784. spin_lock(&gdev.lock);
  785. list_for_each_entry(gq, &gdev.channels, list) {
  786. if (gq->token == token)
  787. break;
  788. }
  789. /* Fail if we didn't find it */
  790. if (gq == NULL || &gq->list == &gdev.channels) {
  791. spin_unlock(&gdev.lock);
  792. return -ENOENT;
  793. }
  794. if (gq->owner != session) {
  795. spin_unlock(&gdev.lock);
  796. return -EPERM;
  797. }
  798. if (GIPC_PAGES(gq)[1].file == NULL) {
  799. GIPC_PAGES(gq)[1].file = file;
  800. file->private_data = gq;
  801. } else {
  802. spin_unlock(&gdev.lock);
  803. return -EBUSY;
  804. }
  805. /* Hold the lock until we allocate so only one process
  806. * gets the queue */
  807. spin_unlock(&gdev.lock);
  808. DEBUG("GIPC_JOIN token %lld by thread %d\n", token,
  809. current_tsk->pid);
  810. rv = 0;
  811. break;
  812. }
  813. default:
  814. printk(KERN_ALERT "Graphene unknown ioctl %u %lu\n", cmd, arg);
  815. rv = -ENOSYS;
  816. break;
  817. }
  818. return rv;
  819. }
  820. static int gipc_release(struct inode *inode, struct file *file)
  821. {
  822. int free = 0;
  823. struct gipc_queue *gq = (struct gipc_queue *) file->private_data;
  824. struct gipc_pages *gp = GIPC_PAGES(gq);
  825. if (gq) {
  826. struct gipc_pages *local, *other;
  827. spin_lock(&gdev.lock);
  828. if (gp[0].file == file) {
  829. local = &gp[0];
  830. other = &gp[1];
  831. } else {
  832. local = &gp[1];
  833. other = &gp[0];
  834. BUG_ON(local->file != file);
  835. }
  836. release_gipc_pages(local);
  837. /* Detect whether we are the last one out by poisoning the file field */
  838. local->file = FILE_POISON;
  839. if (other->file == FILE_POISON) {
  840. free = 1;
  841. list_del(&gq->list);
  842. }
  843. spin_unlock(&gdev.lock);
  844. }
  845. if (free) {
  846. if (gp[0].sender_buf)
  847. kmem_cache_free(gipc_buf_cachep, gp[0].sender_buf);
  848. if (gp[1].sender_buf)
  849. kmem_cache_free(gipc_buf_cachep, gp[1].sender_buf);
  850. kmem_cache_free(gipc_queue_cachep, gq);
  851. }
  852. file->private_data = NULL;
  853. return 0;
  854. }
  855. static int gipc_open(struct inode *inode, struct file *file)
  856. {
  857. file->private_data = NULL;
  858. return 0;
  859. }
  860. static struct file_operations gipc_fops = {
  861. .owner = THIS_MODULE,
  862. .release = gipc_release,
  863. .open = gipc_open,
  864. .unlocked_ioctl = gipc_ioctl,
  865. .compat_ioctl = gipc_ioctl,
  866. .llseek = noop_llseek,
  867. };
  868. static struct miscdevice gipc_dev = {
  869. .minor = GIPC_MINOR,
  870. .name = "gipc",
  871. .fops = &gipc_fops,
  872. .mode = 0666,
  873. };
  874. static int __init gipc_init(void)
  875. {
  876. int rv = 0;
  877. #if !defined(CONFIG_GRAPHENE_BULK_IPC) && LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
  878. my_do_mmap_pgoff = (do_mmap_pgoff_t)
  879. kallsyms_lookup_name("do_mmap_pgoff");
  880. printk(KERN_ERR "resolved symbol do_mmap_pgoff %p\n", my_do_mmap_pgoff);
  881. if (!my_do_mmap_pgoff) {
  882. printk(KERN_ERR "Graphene error: "
  883. "can't find kernel function do_mmap_pgoff\n");
  884. return -ENOENT;
  885. }
  886. #endif
  887. #ifndef gipc_get_session
  888. my_gipc_get_session = (void *) kallsyms_lookup_name("gipc_get_session");
  889. #endif
  890. #if 0 /* these functions are no longer used, keep here for future use. */
  891. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
  892. my_tlb_gather_mmu = (tlb_gather_mmu_t)
  893. kallsyms_lookup_name("tlb_gather_mmu");
  894. printk(KERN_ERR "resolved symbol tlb_gather_mmu %p\n", my_tlb_gather_mmu);
  895. if (!my_tlb_gather_mmu) {
  896. printk(KERN_ERR "Graphene error: "
  897. "can't find kernel function my_tlb_gather_mmu\n");
  898. return -ENOENT;
  899. }
  900. my_tlb_flush_mmu = (tlb_flush_mmu_t)
  901. kallsyms_lookup_name("tlb_flush_mmu");
  902. if (!my_tlb_flush_mmu) {
  903. printk(KERN_ERR "Graphene error: "
  904. "can't find kernel function my_tlb_flush_mmu\n");
  905. return -ENOENT;
  906. }
  907. my_tlb_finish_mmu = (tlb_finish_mmu_t)
  908. kallsyms_lookup_name("tlb_finish_mmu");
  909. if (!my_tlb_finish_mmu) {
  910. printk(KERN_ERR "Graphene error: "
  911. "can't find kernel function my_tlb_finish_mmu\n");
  912. return -ENOENT;
  913. }
  914. #else
  915. pmmu_gathers = (struct mmu_gather *)
  916. kallsyms_lookup_name("mmu_gathers");
  917. if (!pmmu_gathers) {
  918. printk(KERN_ERR "Graphene error: "
  919. "can't find kernel function mmu_gathers\n");
  920. return -ENOENT;
  921. }
  922. #endif /* kernel_version < 3.2 */
  923. kern_free_pages_and_swap_cachep = (free_pages_and_swap_cache_t)
  924. kallsyms_lookup_name("free_pages_and_swap_cache");
  925. if (!kern_free_pages_and_swap_cachep) {
  926. printk(KERN_ERR "Graphene error: "
  927. "can't find kernel function free_pages_and_swap_cache\n");
  928. return -ENOENT;
  929. }
  930. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0)
  931. kern_flush_tlb_mm = (flush_tlb_mm_t)
  932. kallsyms_lookup_name("flush_tlb_mm");
  933. if (!kern_flush_tlb_mm) {
  934. printk(KERN_ERR "Graphene error: "
  935. "can't find kernel function flush_tlb_mm\n");
  936. return -ENOENT;
  937. }
  938. #endif
  939. kern_free_pgtables = (free_pgtables_t)
  940. kallsyms_lookup_name("free_pgtables");
  941. if (!kern_free_pgtables) {
  942. printk(KERN_ERR "Graphene error: "
  943. "can't find kernel function free_pgtables\n");
  944. return -ENOENT;
  945. }
  946. #endif
  947. /* Register the kmem cache */
  948. gipc_queue_cachep = kmem_cache_create("gipc_queues",
  949. sizeof(struct gipc_queue) +
  950. sizeof(struct gipc_pages) * 2,
  951. 0,
  952. SLAB_HWCACHE_ALIGN|
  953. SLAB_DESTROY_BY_RCU,
  954. NULL);
  955. if (!gipc_queue_cachep) {
  956. printk(KERN_ERR "Graphene error: "
  957. "failed to create a gipc queues cache\n");
  958. return -ENOMEM;
  959. }
  960. gipc_buf_cachep = kmem_cache_create("gipc_bufs",
  961. sizeof(struct gipc_sender_buf),
  962. 0,
  963. SLAB_HWCACHE_ALIGN|
  964. SLAB_DESTROY_BY_RCU,
  965. NULL);
  966. if (!gipc_buf_cachep) {
  967. printk(KERN_ERR "Graphene error: "
  968. "failed to create a gipc buffers cache\n");
  969. return -ENOMEM;
  970. }
  971. INIT_LIST_HEAD(&gdev.channels);
  972. spin_lock_init(&gdev.lock);
  973. gdev.max_token = 1;
  974. rv = misc_register(&gipc_dev);
  975. if (rv) {
  976. printk(KERN_ERR "Graphene error: "
  977. "failed to add a char device (rv=%d)\n", rv);
  978. return rv;
  979. }
  980. printk(KERN_ALERT "Graphene IPC: Hello, world\n");
  981. return 0;
  982. }
  983. static void __exit gipc_exit(void)
  984. {
  985. struct gipc_queue *gq, *n;
  986. spin_lock(&gdev.lock);
  987. list_for_each_entry_safe(gq, n, &gdev.channels, list) {
  988. release_gipc_pages(&GIPC_PAGES(gq)[0]);
  989. release_gipc_pages(&GIPC_PAGES(gq)[1]);
  990. list_del(&gq->list);
  991. kmem_cache_free(gipc_queue_cachep, gq);
  992. }
  993. spin_unlock(&gdev.lock);
  994. misc_deregister(&gipc_dev);
  995. kmem_cache_destroy(gipc_queue_cachep);
  996. printk(KERN_ALERT "Graphene IPC: Goodbye, cruel world\n");
  997. }
  998. module_init(gipc_init);
  999. module_exit(gipc_exit);