sys_m68k.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. struct vm_area_struct *vma;
  359. int ret = -EINVAL;
  360. if (scope < FLUSH_SCOPE_LINE || scope > FLUSH_SCOPE_ALL ||
  361. cache & ~FLUSH_CACHE_BOTH)
  362. goto out;
  363. if (scope == FLUSH_SCOPE_ALL) {
  364. /* Only the superuser may explicitly flush the whole cache. */
  365. ret = -EPERM;
  366. if (!capable(CAP_SYS_ADMIN))
  367. goto out;
  368. } else {
  369. /*
  370. * Verify that the specified address region actually belongs
  371. * to this process.
  372. */
  373. vma = find_vma (current->mm, addr);
  374. ret = -EINVAL;
  375. /* Check for overflow. */
  376. if (addr + len < addr)
  377. goto out;
  378. if (vma == NULL || addr < vma->vm_start || addr + len > vma->vm_end)
  379. goto out;
  380. }
  381. if (CPU_IS_020_OR_030) {
  382. if (scope == FLUSH_SCOPE_LINE && len < 256) {
  383. unsigned long cacr;
  384. __asm__ ("movec %%cacr, %0" : "=r" (cacr));
  385. if (cache & FLUSH_CACHE_INSN)
  386. cacr |= 4;
  387. if (cache & FLUSH_CACHE_DATA)
  388. cacr |= 0x400;
  389. len >>= 2;
  390. while (len--) {
  391. __asm__ __volatile__ ("movec %1, %%caar\n\t"
  392. "movec %0, %%cacr"
  393. : /* no outputs */
  394. : "r" (cacr), "r" (addr));
  395. addr += 4;
  396. }
  397. } else {
  398. /* Flush the whole cache, even if page granularity requested. */
  399. unsigned long cacr;
  400. __asm__ ("movec %%cacr, %0" : "=r" (cacr));
  401. if (cache & FLUSH_CACHE_INSN)
  402. cacr |= 8;
  403. if (cache & FLUSH_CACHE_DATA)
  404. cacr |= 0x800;
  405. __asm__ __volatile__ ("movec %0, %%cacr" : : "r" (cacr));
  406. }
  407. ret = 0;
  408. goto out;
  409. } else {
  410. /*
  411. * 040 or 060: don't blindly trust 'scope', someone could
  412. * try to flush a few megs of memory.
  413. */
  414. if (len>=3*PAGE_SIZE && scope<FLUSH_SCOPE_PAGE)
  415. scope=FLUSH_SCOPE_PAGE;
  416. if (len>=10*PAGE_SIZE && scope<FLUSH_SCOPE_ALL)
  417. scope=FLUSH_SCOPE_ALL;
  418. if (CPU_IS_040) {
  419. ret = cache_flush_040 (addr, scope, cache, len);
  420. } else if (CPU_IS_060) {
  421. ret = cache_flush_060 (addr, scope, cache, len);
  422. }
  423. }
  424. out:
  425. return ret;
  426. }
  427. /* This syscall gets its arguments in A0 (mem), D2 (oldval) and
  428. D1 (newval). */
  429. asmlinkage int
  430. sys_atomic_cmpxchg_32(unsigned long newval, int oldval, int d3, int d4, int d5,
  431. unsigned long __user * mem)
  432. {
  433. /* This was borrowed from ARM's implementation. */
  434. for (;;) {
  435. struct mm_struct *mm = current->mm;
  436. pgd_t *pgd;
  437. pmd_t *pmd;
  438. pte_t *pte;
  439. spinlock_t *ptl;
  440. unsigned long mem_value;
  441. down_read(&mm->mmap_sem);
  442. pgd = pgd_offset(mm, (unsigned long)mem);
  443. if (!pgd_present(*pgd))
  444. goto bad_access;
  445. pmd = pmd_offset(pgd, (unsigned long)mem);
  446. if (!pmd_present(*pmd))
  447. goto bad_access;
  448. pte = pte_offset_map_lock(mm, pmd, (unsigned long)mem, &ptl);
  449. if (!pte_present(*pte) || !pte_dirty(*pte)
  450. || !pte_write(*pte)) {
  451. pte_unmap_unlock(pte, ptl);
  452. goto bad_access;
  453. }
  454. /*
  455. * No need to check for EFAULT; we know that the page is
  456. * present and writable.
  457. */
  458. __get_user(mem_value, mem);
  459. if (mem_value == oldval)
  460. __put_user(newval, mem);
  461. pte_unmap_unlock(pte, ptl);
  462. up_read(&mm->mmap_sem);
  463. return mem_value;
  464. bad_access:
  465. up_read(&mm->mmap_sem);
  466. /* This is not necessarily a bad access, we can get here if
  467. a memory we're trying to write to should be copied-on-write.
  468. Make the kernel do the necessary page stuff, then re-iterate.
  469. Simulate a write access fault to do that. */
  470. {
  471. /* The first argument of the function corresponds to
  472. D1, which is the first field of struct pt_regs. */
  473. struct pt_regs *fp = (struct pt_regs *)&newval;
  474. /* '3' is an RMW flag. */
  475. if (do_page_fault(fp, (unsigned long)mem, 3))
  476. /* If the do_page_fault() failed, we don't
  477. have anything meaningful to return.
  478. There should be a SIGSEGV pending for
  479. the process. */
  480. return 0xdeadbeef;
  481. }
  482. }
  483. }
  484. #else
  485. /* sys_cacheflush -- flush (part of) the processor cache. */
  486. asmlinkage int
  487. sys_cacheflush (unsigned long addr, int scope, int cache, unsigned long len)
  488. {
  489. flush_cache_all();
  490. return 0;
  491. }
  492. /* This syscall gets its arguments in A0 (mem), D2 (oldval) and
  493. D1 (newval). */
  494. asmlinkage int
  495. sys_atomic_cmpxchg_32(unsigned long newval, int oldval, int d3, int d4, int d5,
  496. unsigned long __user * mem)
  497. {
  498. struct mm_struct *mm = current->mm;
  499. unsigned long mem_value;
  500. down_read(&mm->mmap_sem);
  501. mem_value = *mem;
  502. if (mem_value == oldval)
  503. *mem = newval;
  504. up_read(&mm->mmap_sem);
  505. return mem_value;
  506. }
  507. #endif /* CONFIG_MMU */
  508. asmlinkage int sys_getpagesize(void)
  509. {
  510. return PAGE_SIZE;
  511. }
  512. /*
  513. * Do a system call from kernel instead of calling sys_execve so we
  514. * end up with proper pt_regs.
  515. */
  516. int kernel_execve(const char *filename,
  517. const char *const argv[],
  518. const char *const envp[])
  519. {
  520. register long __res asm ("%d0") = __NR_execve;
  521. register long __a asm ("%d1") = (long)(filename);
  522. register long __b asm ("%d2") = (long)(argv);
  523. register long __c asm ("%d3") = (long)(envp);
  524. asm volatile ("trap #0" : "+d" (__res)
  525. : "d" (__a), "d" (__b), "d" (__c));
  526. return __res;
  527. }
  528. asmlinkage unsigned long sys_get_thread_area(void)
  529. {
  530. return current_thread_info()->tp_value;
  531. }
  532. asmlinkage int sys_set_thread_area(unsigned long tp)
  533. {
  534. current_thread_info()->tp_value = tp;
  535. return 0;
  536. }
  537. asmlinkage int sys_atomic_barrier(void)
  538. {
  539. /* no code needed for uniprocs */
  540. return 0;
  541. }