sys_m68k.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * linux/arch/m68k/kernel/sys_m68k.c
  3. *
  4. * This file contains various random system calls that
  5. * have a non-standard calling sequence on the Linux/m68k
  6. * platform.
  7. */
  8. #include <linux/capability.h>
  9. #include <linux/errno.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/fs.h>
  13. #include <linux/smp.h>
  14. #include <linux/sem.h>
  15. #include <linux/msg.h>
  16. #include <linux/shm.h>
  17. #include <linux/stat.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/mman.h>
  20. #include <linux/file.h>
  21. #include <linux/ipc.h>
  22. #include <asm/setup.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/cachectl.h>
  25. #include <asm/traps.h>
  26. #include <asm/page.h>
  27. #include <asm/unistd.h>
  28. #include <asm/cacheflush.h>
  29. #ifdef CONFIG_MMU
  30. #include <asm/tlb.h>
  31. asmlinkage int do_page_fault(struct pt_regs *regs, unsigned long address,
  32. unsigned long error_code);
  33. asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
  34. unsigned long prot, unsigned long flags,
  35. unsigned long fd, unsigned long pgoff)
  36. {
  37. /*
  38. * This is wrong for sun3 - there PAGE_SIZE is 8Kb,
  39. * so we need to shift the argument down by 1; m68k mmap64(3)
  40. * (in libc) expects the last argument of mmap2 in 4Kb units.
  41. */
  42. return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
  43. }
  44. /* Convert virtual (user) address VADDR to physical address PADDR */
  45. #define virt_to_phys_040(vaddr) \
  46. ({ \
  47. unsigned long _mmusr, _paddr; \
  48. \
  49. __asm__ __volatile__ (".chip 68040\n\t" \
  50. "ptestr (%1)\n\t" \
  51. "movec %%mmusr,%0\n\t" \
  52. ".chip 68k" \
  53. : "=r" (_mmusr) \
  54. : "a" (vaddr)); \
  55. _paddr = (_mmusr & MMU_R_040) ? (_mmusr & PAGE_MASK) : 0; \
  56. _paddr; \
  57. })
  58. static inline int
  59. cache_flush_040 (unsigned long addr, int scope, int cache, unsigned long len)
  60. {
  61. unsigned long paddr, i;
  62. switch (scope)
  63. {
  64. case FLUSH_SCOPE_ALL:
  65. switch (cache)
  66. {
  67. case FLUSH_CACHE_DATA:
  68. /* This nop is needed for some broken versions of the 68040. */
  69. __asm__ __volatile__ ("nop\n\t"
  70. ".chip 68040\n\t"
  71. "cpusha %dc\n\t"
  72. ".chip 68k");
  73. break;
  74. case FLUSH_CACHE_INSN:
  75. __asm__ __volatile__ ("nop\n\t"
  76. ".chip 68040\n\t"
  77. "cpusha %ic\n\t"
  78. ".chip 68k");
  79. break;
  80. default:
  81. case FLUSH_CACHE_BOTH:
  82. __asm__ __volatile__ ("nop\n\t"
  83. ".chip 68040\n\t"
  84. "cpusha %bc\n\t"
  85. ".chip 68k");
  86. break;
  87. }
  88. break;
  89. case FLUSH_SCOPE_LINE:
  90. /* Find the physical address of the first mapped page in the
  91. address range. */
  92. if ((paddr = virt_to_phys_040(addr))) {
  93. paddr += addr & ~(PAGE_MASK | 15);
  94. len = (len + (addr & 15) + 15) >> 4;
  95. } else {
  96. unsigned long tmp = PAGE_SIZE - (addr & ~PAGE_MASK);
  97. if (len <= tmp)
  98. return 0;
  99. addr += tmp;
  100. len -= tmp;
  101. tmp = PAGE_SIZE;
  102. for (;;)
  103. {
  104. if ((paddr = virt_to_phys_040(addr)))
  105. break;
  106. if (len <= tmp)
  107. return 0;
  108. addr += tmp;
  109. len -= tmp;
  110. }
  111. len = (len + 15) >> 4;
  112. }
  113. i = (PAGE_SIZE - (paddr & ~PAGE_MASK)) >> 4;
  114. while (len--)
  115. {
  116. switch (cache)
  117. {
  118. case FLUSH_CACHE_DATA:
  119. __asm__ __volatile__ ("nop\n\t"
  120. ".chip 68040\n\t"
  121. "cpushl %%dc,(%0)\n\t"
  122. ".chip 68k"
  123. : : "a" (paddr));
  124. break;
  125. case FLUSH_CACHE_INSN:
  126. __asm__ __volatile__ ("nop\n\t"
  127. ".chip 68040\n\t"
  128. "cpushl %%ic,(%0)\n\t"
  129. ".chip 68k"
  130. : : "a" (paddr));
  131. break;
  132. default:
  133. case FLUSH_CACHE_BOTH:
  134. __asm__ __volatile__ ("nop\n\t"
  135. ".chip 68040\n\t"
  136. "cpushl %%bc,(%0)\n\t"
  137. ".chip 68k"
  138. : : "a" (paddr));
  139. break;
  140. }
  141. if (!--i && len)
  142. {
  143. /*
  144. * No need to page align here since it is done by
  145. * virt_to_phys_040().
  146. */
  147. addr += PAGE_SIZE;
  148. i = PAGE_SIZE / 16;
  149. /* Recompute physical address when crossing a page
  150. boundary. */
  151. for (;;)
  152. {
  153. if ((paddr = virt_to_phys_040(addr)))
  154. break;
  155. if (len <= i)
  156. return 0;
  157. len -= i;
  158. addr += PAGE_SIZE;
  159. }
  160. }
  161. else
  162. paddr += 16;
  163. }
  164. break;
  165. default:
  166. case FLUSH_SCOPE_PAGE:
  167. len += (addr & ~PAGE_MASK) + (PAGE_SIZE - 1);
  168. for (len >>= PAGE_SHIFT; len--; addr += PAGE_SIZE)
  169. {
  170. if (!(paddr = virt_to_phys_040(addr)))
  171. continue;
  172. switch (cache)
  173. {
  174. case FLUSH_CACHE_DATA:
  175. __asm__ __volatile__ ("nop\n\t"
  176. ".chip 68040\n\t"
  177. "cpushp %%dc,(%0)\n\t"
  178. ".chip 68k"
  179. : : "a" (paddr));
  180. break;
  181. case FLUSH_CACHE_INSN:
  182. __asm__ __volatile__ ("nop\n\t"
  183. ".chip 68040\n\t"
  184. "cpushp %%ic,(%0)\n\t"
  185. ".chip 68k"
  186. : : "a" (paddr));
  187. break;
  188. default:
  189. case FLUSH_CACHE_BOTH:
  190. __asm__ __volatile__ ("nop\n\t"
  191. ".chip 68040\n\t"
  192. "cpushp %%bc,(%0)\n\t"
  193. ".chip 68k"
  194. : : "a" (paddr));
  195. break;
  196. }
  197. }
  198. break;
  199. }
  200. return 0;
  201. }
  202. #define virt_to_phys_060(vaddr) \
  203. ({ \
  204. unsigned long paddr; \
  205. __asm__ __volatile__ (".chip 68060\n\t" \
  206. "plpar (%0)\n\t" \
  207. ".chip 68k" \
  208. : "=a" (paddr) \
  209. : "0" (vaddr)); \
  210. (paddr); /* XXX */ \
  211. })
  212. static inline int
  213. cache_flush_060 (unsigned long addr, int scope, int cache, unsigned long len)
  214. {
  215. unsigned long paddr, i;
  216. /*
  217. * 68060 manual says:
  218. * cpush %dc : flush DC, remains valid (with our %cacr setup)
  219. * cpush %ic : invalidate IC
  220. * cpush %bc : flush DC + invalidate IC
  221. */
  222. switch (scope)
  223. {
  224. case FLUSH_SCOPE_ALL:
  225. switch (cache)
  226. {
  227. case FLUSH_CACHE_DATA:
  228. __asm__ __volatile__ (".chip 68060\n\t"
  229. "cpusha %dc\n\t"
  230. ".chip 68k");
  231. break;
  232. case FLUSH_CACHE_INSN:
  233. __asm__ __volatile__ (".chip 68060\n\t"
  234. "cpusha %ic\n\t"
  235. ".chip 68k");
  236. break;
  237. default:
  238. case FLUSH_CACHE_BOTH:
  239. __asm__ __volatile__ (".chip 68060\n\t"
  240. "cpusha %bc\n\t"
  241. ".chip 68k");
  242. break;
  243. }
  244. break;
  245. case FLUSH_SCOPE_LINE:
  246. /* Find the physical address of the first mapped page in the
  247. address range. */
  248. len += addr & 15;
  249. addr &= -16;
  250. if (!(paddr = virt_to_phys_060(addr))) {
  251. unsigned long tmp = PAGE_SIZE - (addr & ~PAGE_MASK);
  252. if (len <= tmp)
  253. return 0;
  254. addr += tmp;
  255. len -= tmp;
  256. tmp = PAGE_SIZE;
  257. for (;;)
  258. {
  259. if ((paddr = virt_to_phys_060(addr)))
  260. break;
  261. if (len <= tmp)
  262. return 0;
  263. addr += tmp;
  264. len -= tmp;
  265. }
  266. }
  267. len = (len + 15) >> 4;
  268. i = (PAGE_SIZE - (paddr & ~PAGE_MASK)) >> 4;
  269. while (len--)
  270. {
  271. switch (cache)
  272. {
  273. case FLUSH_CACHE_DATA:
  274. __asm__ __volatile__ (".chip 68060\n\t"
  275. "cpushl %%dc,(%0)\n\t"
  276. ".chip 68k"
  277. : : "a" (paddr));
  278. break;
  279. case FLUSH_CACHE_INSN:
  280. __asm__ __volatile__ (".chip 68060\n\t"
  281. "cpushl %%ic,(%0)\n\t"
  282. ".chip 68k"
  283. : : "a" (paddr));
  284. break;
  285. default:
  286. case FLUSH_CACHE_BOTH:
  287. __asm__ __volatile__ (".chip 68060\n\t"
  288. "cpushl %%bc,(%0)\n\t"
  289. ".chip 68k"
  290. : : "a" (paddr));
  291. break;
  292. }
  293. if (!--i && len)
  294. {
  295. /*
  296. * We just want to jump to the first cache line
  297. * in the next page.
  298. */
  299. addr += PAGE_SIZE;
  300. addr &= PAGE_MASK;
  301. i = PAGE_SIZE / 16;
  302. /* Recompute physical address when crossing a page
  303. boundary. */
  304. for (;;)
  305. {
  306. if ((paddr = virt_to_phys_060(addr)))
  307. break;
  308. if (len <= i)
  309. return 0;
  310. len -= i;
  311. addr += PAGE_SIZE;
  312. }
  313. }
  314. else
  315. paddr += 16;
  316. }
  317. break;
  318. default:
  319. case FLUSH_SCOPE_PAGE:
  320. len += (addr & ~PAGE_MASK) + (PAGE_SIZE - 1);
  321. addr &= PAGE_MASK; /* Workaround for bug in some
  322. revisions of the 68060 */
  323. for (len >>= PAGE_SHIFT; len--; addr += PAGE_SIZE)
  324. {
  325. if (!(paddr = virt_to_phys_060(addr)))
  326. continue;
  327. switch (cache)
  328. {
  329. case FLUSH_CACHE_DATA:
  330. __asm__ __volatile__ (".chip 68060\n\t"
  331. "cpushp %%dc,(%0)\n\t"
  332. ".chip 68k"
  333. : : "a" (paddr));
  334. break;
  335. case FLUSH_CACHE_INSN:
  336. __asm__ __volatile__ (".chip 68060\n\t"
  337. "cpushp %%ic,(%0)\n\t"
  338. ".chip 68k"
  339. : : "a" (paddr));
  340. break;
  341. default:
  342. case FLUSH_CACHE_BOTH:
  343. __asm__ __volatile__ (".chip 68060\n\t"
  344. "cpushp %%bc,(%0)\n\t"
  345. ".chip 68k"
  346. : : "a" (paddr));
  347. break;
  348. }
  349. }
  350. break;
  351. }
  352. return 0;
  353. }
  354. /* sys_cacheflush -- flush (part of) the processor cache. */
  355. asmlinkage int
  356. sys_cacheflush (unsigned long addr, int scope, int cache, unsigned long len)
  357. {
  358. int ret = -EINVAL;
  359. if (scope < FLUSH_SCOPE_LINE || scope > FLUSH_SCOPE_ALL ||
  360. cache & ~FLUSH_CACHE_BOTH)
  361. goto out;
  362. if (scope == FLUSH_SCOPE_ALL) {
  363. /* Only the superuser may explicitly flush the whole cache. */
  364. ret = -EPERM;
  365. if (!capable(CAP_SYS_ADMIN))
  366. goto out;
  367. } else {
  368. struct vm_area_struct *vma;
  369. /* Check for overflow. */
  370. if (addr + len < addr)
  371. goto out;
  372. /*
  373. * Verify that the specified address region actually belongs
  374. * to this process.
  375. */
  376. ret = -EINVAL;
  377. down_read(&current->mm->mmap_sem);
  378. vma = find_vma(current->mm, addr);
  379. if (!vma || addr < vma->vm_start || addr + len > vma->vm_end)
  380. goto out_unlock;
  381. }
  382. if (CPU_IS_020_OR_030) {
  383. if (scope == FLUSH_SCOPE_LINE && len < 256) {
  384. unsigned long cacr;
  385. __asm__ ("movec %%cacr, %0" : "=r" (cacr));
  386. if (cache & FLUSH_CACHE_INSN)
  387. cacr |= 4;
  388. if (cache & FLUSH_CACHE_DATA)
  389. cacr |= 0x400;
  390. len >>= 2;
  391. while (len--) {
  392. __asm__ __volatile__ ("movec %1, %%caar\n\t"
  393. "movec %0, %%cacr"
  394. : /* no outputs */
  395. : "r" (cacr), "r" (addr));
  396. addr += 4;
  397. }
  398. } else {
  399. /* Flush the whole cache, even if page granularity requested. */
  400. unsigned long cacr;
  401. __asm__ ("movec %%cacr, %0" : "=r" (cacr));
  402. if (cache & FLUSH_CACHE_INSN)
  403. cacr |= 8;
  404. if (cache & FLUSH_CACHE_DATA)
  405. cacr |= 0x800;
  406. __asm__ __volatile__ ("movec %0, %%cacr" : : "r" (cacr));
  407. }
  408. ret = 0;
  409. goto out_unlock;
  410. } else {
  411. /*
  412. * 040 or 060: don't blindly trust 'scope', someone could
  413. * try to flush a few megs of memory.
  414. */
  415. if (len>=3*PAGE_SIZE && scope<FLUSH_SCOPE_PAGE)
  416. scope=FLUSH_SCOPE_PAGE;
  417. if (len>=10*PAGE_SIZE && scope<FLUSH_SCOPE_ALL)
  418. scope=FLUSH_SCOPE_ALL;
  419. if (CPU_IS_040) {
  420. ret = cache_flush_040 (addr, scope, cache, len);
  421. } else if (CPU_IS_060) {
  422. ret = cache_flush_060 (addr, scope, cache, len);
  423. }
  424. }
  425. out_unlock:
  426. up_read(&current->mm->mmap_sem);
  427. out:
  428. return ret;
  429. }
  430. /* This syscall gets its arguments in A0 (mem), D2 (oldval) and
  431. D1 (newval). */
  432. asmlinkage int
  433. sys_atomic_cmpxchg_32(unsigned long newval, int oldval, int d3, int d4, int d5,
  434. unsigned long __user * mem)
  435. {
  436. /* This was borrowed from ARM's implementation. */
  437. for (;;) {
  438. struct mm_struct *mm = current->mm;
  439. pgd_t *pgd;
  440. pmd_t *pmd;
  441. pte_t *pte;
  442. spinlock_t *ptl;
  443. unsigned long mem_value;
  444. down_read(&mm->mmap_sem);
  445. pgd = pgd_offset(mm, (unsigned long)mem);
  446. if (!pgd_present(*pgd))
  447. goto bad_access;
  448. pmd = pmd_offset(pgd, (unsigned long)mem);
  449. if (!pmd_present(*pmd))
  450. goto bad_access;
  451. pte = pte_offset_map_lock(mm, pmd, (unsigned long)mem, &ptl);
  452. if (!pte_present(*pte) || !pte_dirty(*pte)
  453. || !pte_write(*pte)) {
  454. pte_unmap_unlock(pte, ptl);
  455. goto bad_access;
  456. }
  457. /*
  458. * No need to check for EFAULT; we know that the page is
  459. * present and writable.
  460. */
  461. __get_user(mem_value, mem);
  462. if (mem_value == oldval)
  463. __put_user(newval, mem);
  464. pte_unmap_unlock(pte, ptl);
  465. up_read(&mm->mmap_sem);
  466. return mem_value;
  467. bad_access:
  468. up_read(&mm->mmap_sem);
  469. /* This is not necessarily a bad access, we can get here if
  470. a memory we're trying to write to should be copied-on-write.
  471. Make the kernel do the necessary page stuff, then re-iterate.
  472. Simulate a write access fault to do that. */
  473. {
  474. /* The first argument of the function corresponds to
  475. D1, which is the first field of struct pt_regs. */
  476. struct pt_regs *fp = (struct pt_regs *)&newval;
  477. /* '3' is an RMW flag. */
  478. if (do_page_fault(fp, (unsigned long)mem, 3))
  479. /* If the do_page_fault() failed, we don't
  480. have anything meaningful to return.
  481. There should be a SIGSEGV pending for
  482. the process. */
  483. return 0xdeadbeef;
  484. }
  485. }
  486. }
  487. #else
  488. /* sys_cacheflush -- flush (part of) the processor cache. */
  489. asmlinkage int
  490. sys_cacheflush (unsigned long addr, int scope, int cache, unsigned long len)
  491. {
  492. flush_cache_all();
  493. return 0;
  494. }
  495. /* This syscall gets its arguments in A0 (mem), D2 (oldval) and
  496. D1 (newval). */
  497. asmlinkage int
  498. sys_atomic_cmpxchg_32(unsigned long newval, int oldval, int d3, int d4, int d5,
  499. unsigned long __user * mem)
  500. {
  501. struct mm_struct *mm = current->mm;
  502. unsigned long mem_value;
  503. down_read(&mm->mmap_sem);
  504. mem_value = *mem;
  505. if (mem_value == oldval)
  506. *mem = newval;
  507. up_read(&mm->mmap_sem);
  508. return mem_value;
  509. }
  510. #endif /* CONFIG_MMU */
  511. asmlinkage int sys_getpagesize(void)
  512. {
  513. return PAGE_SIZE;
  514. }
  515. asmlinkage unsigned long sys_get_thread_area(void)
  516. {
  517. return current_thread_info()->tp_value;
  518. }
  519. asmlinkage int sys_set_thread_area(unsigned long tp)
  520. {
  521. current_thread_info()->tp_value = tp;
  522. return 0;
  523. }
  524. asmlinkage int sys_atomic_barrier(void)
  525. {
  526. /* no code needed for uniprocs */
  527. return 0;
  528. }