sys_sparc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /* $Id: sys_sparc.c,v 1.57 2002/02/09 19:49:30 davem Exp $
  2. * linux/arch/sparc64/kernel/sys_sparc.c
  3. *
  4. * This file contains various random system calls that
  5. * have a non-standard calling sequence on the Linux/sparc
  6. * platform.
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/types.h>
  10. #include <linux/sched.h>
  11. #include <linux/fs.h>
  12. #include <linux/file.h>
  13. #include <linux/mm.h>
  14. #include <linux/sem.h>
  15. #include <linux/msg.h>
  16. #include <linux/shm.h>
  17. #include <linux/stat.h>
  18. #include <linux/mman.h>
  19. #include <linux/utsname.h>
  20. #include <linux/smp.h>
  21. #include <linux/slab.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/ipc.h>
  24. #include <linux/personality.h>
  25. #include <linux/random.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/utrap.h>
  28. #include <asm/perfctr.h>
  29. #include <asm/unistd.h>
  30. /* #define DEBUG_UNIMP_SYSCALL */
  31. asmlinkage unsigned long sys_getpagesize(void)
  32. {
  33. return PAGE_SIZE;
  34. }
  35. #define VA_EXCLUDE_START (0x0000080000000000UL - (1UL << 32UL))
  36. #define VA_EXCLUDE_END (0xfffff80000000000UL + (1UL << 32UL))
  37. /* Does addr --> addr+len fall within 4GB of the VA-space hole or
  38. * overflow past the end of the 64-bit address space?
  39. */
  40. static inline int invalid_64bit_range(unsigned long addr, unsigned long len)
  41. {
  42. unsigned long va_exclude_start, va_exclude_end;
  43. va_exclude_start = VA_EXCLUDE_START;
  44. va_exclude_end = VA_EXCLUDE_END;
  45. if (unlikely(len >= va_exclude_start))
  46. return 1;
  47. if (unlikely((addr + len) < addr))
  48. return 1;
  49. if (unlikely((addr >= va_exclude_start && addr < va_exclude_end) ||
  50. ((addr + len) >= va_exclude_start &&
  51. (addr + len) < va_exclude_end)))
  52. return 1;
  53. return 0;
  54. }
  55. /* Does start,end straddle the VA-space hole? */
  56. static inline int straddles_64bit_va_hole(unsigned long start, unsigned long end)
  57. {
  58. unsigned long va_exclude_start, va_exclude_end;
  59. va_exclude_start = VA_EXCLUDE_START;
  60. va_exclude_end = VA_EXCLUDE_END;
  61. if (likely(start < va_exclude_start && end < va_exclude_start))
  62. return 0;
  63. if (likely(start >= va_exclude_end && end >= va_exclude_end))
  64. return 0;
  65. return 1;
  66. }
  67. /* These functions differ from the default implementations in
  68. * mm/mmap.c in two ways:
  69. *
  70. * 1) For file backed MAP_SHARED mmap()'s we D-cache color align,
  71. * for fixed such mappings we just validate what the user gave us.
  72. * 2) For 64-bit tasks we avoid mapping anything within 4GB of
  73. * the spitfire/niagara VA-hole.
  74. */
  75. static inline unsigned long COLOUR_ALIGN(unsigned long addr,
  76. unsigned long pgoff)
  77. {
  78. unsigned long base = (addr+SHMLBA-1)&~(SHMLBA-1);
  79. unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
  80. return base + off;
  81. }
  82. static inline unsigned long COLOUR_ALIGN_DOWN(unsigned long addr,
  83. unsigned long pgoff)
  84. {
  85. unsigned long base = addr & ~(SHMLBA-1);
  86. unsigned long off = (pgoff<<PAGE_SHIFT) & (SHMLBA-1);
  87. if (base + off <= addr)
  88. return base + off;
  89. return base - off;
  90. }
  91. unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
  92. {
  93. struct mm_struct *mm = current->mm;
  94. struct vm_area_struct * vma;
  95. unsigned long task_size = TASK_SIZE;
  96. unsigned long start_addr;
  97. int do_color_align;
  98. if (flags & MAP_FIXED) {
  99. /* We do not accept a shared mapping if it would violate
  100. * cache aliasing constraints.
  101. */
  102. if ((flags & MAP_SHARED) &&
  103. ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
  104. return -EINVAL;
  105. return addr;
  106. }
  107. if (test_thread_flag(TIF_32BIT))
  108. task_size = STACK_TOP32;
  109. if (unlikely(len > task_size || len >= VA_EXCLUDE_START))
  110. return -ENOMEM;
  111. do_color_align = 0;
  112. if (filp || (flags & MAP_SHARED))
  113. do_color_align = 1;
  114. if (addr) {
  115. if (do_color_align)
  116. addr = COLOUR_ALIGN(addr, pgoff);
  117. else
  118. addr = PAGE_ALIGN(addr);
  119. vma = find_vma(mm, addr);
  120. if (task_size - len >= addr &&
  121. (!vma || addr + len <= vma->vm_start))
  122. return addr;
  123. }
  124. if (len > mm->cached_hole_size) {
  125. start_addr = addr = mm->free_area_cache;
  126. } else {
  127. start_addr = addr = TASK_UNMAPPED_BASE;
  128. mm->cached_hole_size = 0;
  129. }
  130. task_size -= len;
  131. full_search:
  132. if (do_color_align)
  133. addr = COLOUR_ALIGN(addr, pgoff);
  134. else
  135. addr = PAGE_ALIGN(addr);
  136. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  137. /* At this point: (!vma || addr < vma->vm_end). */
  138. if (addr < VA_EXCLUDE_START &&
  139. (addr + len) >= VA_EXCLUDE_START) {
  140. addr = VA_EXCLUDE_END;
  141. vma = find_vma(mm, VA_EXCLUDE_END);
  142. }
  143. if (unlikely(task_size < addr)) {
  144. if (start_addr != TASK_UNMAPPED_BASE) {
  145. start_addr = addr = TASK_UNMAPPED_BASE;
  146. mm->cached_hole_size = 0;
  147. goto full_search;
  148. }
  149. return -ENOMEM;
  150. }
  151. if (likely(!vma || addr + len <= vma->vm_start)) {
  152. /*
  153. * Remember the place where we stopped the search:
  154. */
  155. mm->free_area_cache = addr + len;
  156. return addr;
  157. }
  158. if (addr + mm->cached_hole_size < vma->vm_start)
  159. mm->cached_hole_size = vma->vm_start - addr;
  160. addr = vma->vm_end;
  161. if (do_color_align)
  162. addr = COLOUR_ALIGN(addr, pgoff);
  163. }
  164. }
  165. unsigned long
  166. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  167. const unsigned long len, const unsigned long pgoff,
  168. const unsigned long flags)
  169. {
  170. struct vm_area_struct *vma;
  171. struct mm_struct *mm = current->mm;
  172. unsigned long task_size = STACK_TOP32;
  173. unsigned long addr = addr0;
  174. int do_color_align;
  175. /* This should only ever run for 32-bit processes. */
  176. BUG_ON(!test_thread_flag(TIF_32BIT));
  177. if (flags & MAP_FIXED) {
  178. /* We do not accept a shared mapping if it would violate
  179. * cache aliasing constraints.
  180. */
  181. if ((flags & MAP_SHARED) &&
  182. ((addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1)))
  183. return -EINVAL;
  184. return addr;
  185. }
  186. if (unlikely(len > task_size))
  187. return -ENOMEM;
  188. do_color_align = 0;
  189. if (filp || (flags & MAP_SHARED))
  190. do_color_align = 1;
  191. /* requesting a specific address */
  192. if (addr) {
  193. if (do_color_align)
  194. addr = COLOUR_ALIGN(addr, pgoff);
  195. else
  196. addr = PAGE_ALIGN(addr);
  197. vma = find_vma(mm, addr);
  198. if (task_size - len >= addr &&
  199. (!vma || addr + len <= vma->vm_start))
  200. return addr;
  201. }
  202. /* check if free_area_cache is useful for us */
  203. if (len <= mm->cached_hole_size) {
  204. mm->cached_hole_size = 0;
  205. mm->free_area_cache = mm->mmap_base;
  206. }
  207. /* either no address requested or can't fit in requested address hole */
  208. addr = mm->free_area_cache;
  209. if (do_color_align) {
  210. unsigned long base = COLOUR_ALIGN_DOWN(addr-len, pgoff);
  211. addr = base + len;
  212. }
  213. /* make sure it can fit in the remaining address space */
  214. if (likely(addr > len)) {
  215. vma = find_vma(mm, addr-len);
  216. if (!vma || addr <= vma->vm_start) {
  217. /* remember the address as a hint for next time */
  218. return (mm->free_area_cache = addr-len);
  219. }
  220. }
  221. if (unlikely(mm->mmap_base < len))
  222. goto bottomup;
  223. addr = mm->mmap_base-len;
  224. if (do_color_align)
  225. addr = COLOUR_ALIGN_DOWN(addr, pgoff);
  226. do {
  227. /*
  228. * Lookup failure means no vma is above this address,
  229. * else if new region fits below vma->vm_start,
  230. * return with success:
  231. */
  232. vma = find_vma(mm, addr);
  233. if (likely(!vma || addr+len <= vma->vm_start)) {
  234. /* remember the address as a hint for next time */
  235. return (mm->free_area_cache = addr);
  236. }
  237. /* remember the largest hole we saw so far */
  238. if (addr + mm->cached_hole_size < vma->vm_start)
  239. mm->cached_hole_size = vma->vm_start - addr;
  240. /* try just below the current vma->vm_start */
  241. addr = vma->vm_start-len;
  242. if (do_color_align)
  243. addr = COLOUR_ALIGN_DOWN(addr, pgoff);
  244. } while (likely(len < vma->vm_start));
  245. bottomup:
  246. /*
  247. * A failed mmap() very likely causes application failure,
  248. * so fall back to the bottom-up function here. This scenario
  249. * can happen with large stack limits and large mmap()
  250. * allocations.
  251. */
  252. mm->cached_hole_size = ~0UL;
  253. mm->free_area_cache = TASK_UNMAPPED_BASE;
  254. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  255. /*
  256. * Restore the topdown base:
  257. */
  258. mm->free_area_cache = mm->mmap_base;
  259. mm->cached_hole_size = ~0UL;
  260. return addr;
  261. }
  262. /* Try to align mapping such that we align it as much as possible. */
  263. unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags)
  264. {
  265. unsigned long align_goal, addr = -ENOMEM;
  266. if (flags & MAP_FIXED) {
  267. /* Ok, don't mess with it. */
  268. return get_unmapped_area(NULL, orig_addr, len, pgoff, flags);
  269. }
  270. flags &= ~MAP_SHARED;
  271. align_goal = PAGE_SIZE;
  272. if (len >= (4UL * 1024 * 1024))
  273. align_goal = (4UL * 1024 * 1024);
  274. else if (len >= (512UL * 1024))
  275. align_goal = (512UL * 1024);
  276. else if (len >= (64UL * 1024))
  277. align_goal = (64UL * 1024);
  278. do {
  279. addr = get_unmapped_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags);
  280. if (!(addr & ~PAGE_MASK)) {
  281. addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL);
  282. break;
  283. }
  284. if (align_goal == (4UL * 1024 * 1024))
  285. align_goal = (512UL * 1024);
  286. else if (align_goal == (512UL * 1024))
  287. align_goal = (64UL * 1024);
  288. else
  289. align_goal = PAGE_SIZE;
  290. } while ((addr & ~PAGE_MASK) && align_goal > PAGE_SIZE);
  291. /* Mapping is smaller than 64K or larger areas could not
  292. * be obtained.
  293. */
  294. if (addr & ~PAGE_MASK)
  295. addr = get_unmapped_area(NULL, orig_addr, len, pgoff, flags);
  296. return addr;
  297. }
  298. /* Essentially the same as PowerPC... */
  299. void arch_pick_mmap_layout(struct mm_struct *mm)
  300. {
  301. unsigned long random_factor = 0UL;
  302. if (current->flags & PF_RANDOMIZE) {
  303. random_factor = get_random_int();
  304. if (test_thread_flag(TIF_32BIT))
  305. random_factor &= ((1 * 1024 * 1024) - 1);
  306. else
  307. random_factor = ((random_factor << PAGE_SHIFT) &
  308. 0xffffffffUL);
  309. }
  310. /*
  311. * Fall back to the standard layout if the personality
  312. * bit is set, or if the expected stack growth is unlimited:
  313. */
  314. if (!test_thread_flag(TIF_32BIT) ||
  315. (current->personality & ADDR_COMPAT_LAYOUT) ||
  316. current->signal->rlim[RLIMIT_STACK].rlim_cur == RLIM_INFINITY ||
  317. sysctl_legacy_va_layout) {
  318. mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
  319. mm->get_unmapped_area = arch_get_unmapped_area;
  320. mm->unmap_area = arch_unmap_area;
  321. } else {
  322. /* We know it's 32-bit */
  323. unsigned long task_size = STACK_TOP32;
  324. unsigned long gap;
  325. gap = current->signal->rlim[RLIMIT_STACK].rlim_cur;
  326. if (gap < 128 * 1024 * 1024)
  327. gap = 128 * 1024 * 1024;
  328. if (gap > (task_size / 6 * 5))
  329. gap = (task_size / 6 * 5);
  330. mm->mmap_base = PAGE_ALIGN(task_size - gap - random_factor);
  331. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  332. mm->unmap_area = arch_unmap_area_topdown;
  333. }
  334. }
  335. asmlinkage unsigned long sparc_brk(unsigned long brk)
  336. {
  337. /* People could try to be nasty and use ta 0x6d in 32bit programs */
  338. if (test_thread_flag(TIF_32BIT) && brk >= STACK_TOP32)
  339. return current->mm->brk;
  340. if (unlikely(straddles_64bit_va_hole(current->mm->brk, brk)))
  341. return current->mm->brk;
  342. return sys_brk(brk);
  343. }
  344. /*
  345. * sys_pipe() is the normal C calling standard for creating
  346. * a pipe. It's not the way unix traditionally does this, though.
  347. */
  348. asmlinkage long sparc_pipe(struct pt_regs *regs)
  349. {
  350. int fd[2];
  351. int error;
  352. error = do_pipe(fd);
  353. if (error)
  354. goto out;
  355. regs->u_regs[UREG_I1] = fd[1];
  356. error = fd[0];
  357. out:
  358. return error;
  359. }
  360. /*
  361. * sys_ipc() is the de-multiplexer for the SysV IPC calls..
  362. *
  363. * This is really horribly ugly.
  364. */
  365. asmlinkage long sys_ipc(unsigned int call, int first, unsigned long second,
  366. unsigned long third, void __user *ptr, long fifth)
  367. {
  368. long err;
  369. /* No need for backward compatibility. We can start fresh... */
  370. if (call <= SEMCTL) {
  371. switch (call) {
  372. case SEMOP:
  373. err = sys_semtimedop(first, ptr,
  374. (unsigned)second, NULL);
  375. goto out;
  376. case SEMTIMEDOP:
  377. err = sys_semtimedop(first, ptr, (unsigned)second,
  378. (const struct timespec __user *) fifth);
  379. goto out;
  380. case SEMGET:
  381. err = sys_semget(first, (int)second, (int)third);
  382. goto out;
  383. case SEMCTL: {
  384. err = sys_semctl(first, third,
  385. (int)second | IPC_64,
  386. (union semun) ptr);
  387. goto out;
  388. }
  389. default:
  390. err = -ENOSYS;
  391. goto out;
  392. };
  393. }
  394. if (call <= MSGCTL) {
  395. switch (call) {
  396. case MSGSND:
  397. err = sys_msgsnd(first, ptr, (size_t)second,
  398. (int)third);
  399. goto out;
  400. case MSGRCV:
  401. err = sys_msgrcv(first, ptr, (size_t)second, fifth,
  402. (int)third);
  403. goto out;
  404. case MSGGET:
  405. err = sys_msgget((key_t)first, (int)second);
  406. goto out;
  407. case MSGCTL:
  408. err = sys_msgctl(first, (int)second | IPC_64, ptr);
  409. goto out;
  410. default:
  411. err = -ENOSYS;
  412. goto out;
  413. };
  414. }
  415. if (call <= SHMCTL) {
  416. switch (call) {
  417. case SHMAT: {
  418. ulong raddr;
  419. err = do_shmat(first, ptr, (int)second, &raddr);
  420. if (!err) {
  421. if (put_user(raddr,
  422. (ulong __user *) third))
  423. err = -EFAULT;
  424. }
  425. goto out;
  426. }
  427. case SHMDT:
  428. err = sys_shmdt(ptr);
  429. goto out;
  430. case SHMGET:
  431. err = sys_shmget(first, (size_t)second, (int)third);
  432. goto out;
  433. case SHMCTL:
  434. err = sys_shmctl(first, (int)second | IPC_64, ptr);
  435. goto out;
  436. default:
  437. err = -ENOSYS;
  438. goto out;
  439. };
  440. } else {
  441. err = -ENOSYS;
  442. }
  443. out:
  444. return err;
  445. }
  446. asmlinkage long sparc64_newuname(struct new_utsname __user *name)
  447. {
  448. int ret = sys_newuname(name);
  449. if (current->personality == PER_LINUX32 && !ret) {
  450. ret = (copy_to_user(name->machine, "sparc\0\0", 8)
  451. ? -EFAULT : 0);
  452. }
  453. return ret;
  454. }
  455. asmlinkage long sparc64_personality(unsigned long personality)
  456. {
  457. int ret;
  458. if (current->personality == PER_LINUX32 &&
  459. personality == PER_LINUX)
  460. personality = PER_LINUX32;
  461. ret = sys_personality(personality);
  462. if (ret == PER_LINUX32)
  463. ret = PER_LINUX;
  464. return ret;
  465. }
  466. int sparc64_mmap_check(unsigned long addr, unsigned long len,
  467. unsigned long flags)
  468. {
  469. if (test_thread_flag(TIF_32BIT)) {
  470. if (len >= STACK_TOP32)
  471. return -EINVAL;
  472. if ((flags & MAP_FIXED) && addr > STACK_TOP32 - len)
  473. return -EINVAL;
  474. } else {
  475. if (len >= VA_EXCLUDE_START)
  476. return -EINVAL;
  477. if ((flags & MAP_FIXED) && invalid_64bit_range(addr, len))
  478. return -EINVAL;
  479. }
  480. return 0;
  481. }
  482. /* Linux version of mmap */
  483. asmlinkage unsigned long sys_mmap(unsigned long addr, unsigned long len,
  484. unsigned long prot, unsigned long flags, unsigned long fd,
  485. unsigned long off)
  486. {
  487. struct file * file = NULL;
  488. unsigned long retval = -EBADF;
  489. if (!(flags & MAP_ANONYMOUS)) {
  490. file = fget(fd);
  491. if (!file)
  492. goto out;
  493. }
  494. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  495. len = PAGE_ALIGN(len);
  496. down_write(&current->mm->mmap_sem);
  497. retval = do_mmap(file, addr, len, prot, flags, off);
  498. up_write(&current->mm->mmap_sem);
  499. if (file)
  500. fput(file);
  501. out:
  502. return retval;
  503. }
  504. asmlinkage long sys64_munmap(unsigned long addr, size_t len)
  505. {
  506. long ret;
  507. if (invalid_64bit_range(addr, len))
  508. return -EINVAL;
  509. down_write(&current->mm->mmap_sem);
  510. ret = do_munmap(current->mm, addr, len);
  511. up_write(&current->mm->mmap_sem);
  512. return ret;
  513. }
  514. extern unsigned long do_mremap(unsigned long addr,
  515. unsigned long old_len, unsigned long new_len,
  516. unsigned long flags, unsigned long new_addr);
  517. asmlinkage unsigned long sys64_mremap(unsigned long addr,
  518. unsigned long old_len, unsigned long new_len,
  519. unsigned long flags, unsigned long new_addr)
  520. {
  521. struct vm_area_struct *vma;
  522. unsigned long ret = -EINVAL;
  523. if (test_thread_flag(TIF_32BIT))
  524. goto out;
  525. if (unlikely(new_len >= VA_EXCLUDE_START))
  526. goto out;
  527. if (unlikely(invalid_64bit_range(addr, old_len)))
  528. goto out;
  529. down_write(&current->mm->mmap_sem);
  530. if (flags & MREMAP_FIXED) {
  531. if (invalid_64bit_range(new_addr, new_len))
  532. goto out_sem;
  533. } else if (invalid_64bit_range(addr, new_len)) {
  534. unsigned long map_flags = 0;
  535. struct file *file = NULL;
  536. ret = -ENOMEM;
  537. if (!(flags & MREMAP_MAYMOVE))
  538. goto out_sem;
  539. vma = find_vma(current->mm, addr);
  540. if (vma) {
  541. if (vma->vm_flags & VM_SHARED)
  542. map_flags |= MAP_SHARED;
  543. file = vma->vm_file;
  544. }
  545. /* MREMAP_FIXED checked above. */
  546. new_addr = get_unmapped_area(file, addr, new_len,
  547. vma ? vma->vm_pgoff : 0,
  548. map_flags);
  549. ret = new_addr;
  550. if (new_addr & ~PAGE_MASK)
  551. goto out_sem;
  552. flags |= MREMAP_FIXED;
  553. }
  554. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  555. out_sem:
  556. up_write(&current->mm->mmap_sem);
  557. out:
  558. return ret;
  559. }
  560. /* we come to here via sys_nis_syscall so it can setup the regs argument */
  561. asmlinkage unsigned long c_sys_nis_syscall(struct pt_regs *regs)
  562. {
  563. static int count;
  564. /* Don't make the system unusable, if someone goes stuck */
  565. if (count++ > 5)
  566. return -ENOSYS;
  567. printk ("Unimplemented SPARC system call %ld\n",regs->u_regs[1]);
  568. #ifdef DEBUG_UNIMP_SYSCALL
  569. show_regs (regs);
  570. #endif
  571. return -ENOSYS;
  572. }
  573. /* #define DEBUG_SPARC_BREAKPOINT */
  574. asmlinkage void sparc_breakpoint(struct pt_regs *regs)
  575. {
  576. siginfo_t info;
  577. if (test_thread_flag(TIF_32BIT)) {
  578. regs->tpc &= 0xffffffff;
  579. regs->tnpc &= 0xffffffff;
  580. }
  581. #ifdef DEBUG_SPARC_BREAKPOINT
  582. printk ("TRAP: Entering kernel PC=%lx, nPC=%lx\n", regs->tpc, regs->tnpc);
  583. #endif
  584. info.si_signo = SIGTRAP;
  585. info.si_errno = 0;
  586. info.si_code = TRAP_BRKPT;
  587. info.si_addr = (void __user *)regs->tpc;
  588. info.si_trapno = 0;
  589. force_sig_info(SIGTRAP, &info, current);
  590. #ifdef DEBUG_SPARC_BREAKPOINT
  591. printk ("TRAP: Returning to space: PC=%lx nPC=%lx\n", regs->tpc, regs->tnpc);
  592. #endif
  593. }
  594. extern void check_pending(int signum);
  595. asmlinkage long sys_getdomainname(char __user *name, int len)
  596. {
  597. int nlen, err;
  598. if (len < 0)
  599. return -EINVAL;
  600. down_read(&uts_sem);
  601. nlen = strlen(utsname()->domainname) + 1;
  602. err = -EINVAL;
  603. if (nlen > len)
  604. goto out;
  605. err = -EFAULT;
  606. if (!copy_to_user(name, utsname()->domainname, nlen))
  607. err = 0;
  608. out:
  609. up_read(&uts_sem);
  610. return err;
  611. }
  612. asmlinkage long solaris_syscall(struct pt_regs *regs)
  613. {
  614. static int count;
  615. regs->tpc = regs->tnpc;
  616. regs->tnpc += 4;
  617. if (test_thread_flag(TIF_32BIT)) {
  618. regs->tpc &= 0xffffffff;
  619. regs->tnpc &= 0xffffffff;
  620. }
  621. if (++count <= 5) {
  622. printk ("For Solaris binary emulation you need solaris module loaded\n");
  623. show_regs (regs);
  624. }
  625. send_sig(SIGSEGV, current, 1);
  626. return -ENOSYS;
  627. }
  628. #ifndef CONFIG_SUNOS_EMUL
  629. asmlinkage long sunos_syscall(struct pt_regs *regs)
  630. {
  631. static int count;
  632. regs->tpc = regs->tnpc;
  633. regs->tnpc += 4;
  634. if (test_thread_flag(TIF_32BIT)) {
  635. regs->tpc &= 0xffffffff;
  636. regs->tnpc &= 0xffffffff;
  637. }
  638. if (++count <= 20)
  639. printk ("SunOS binary emulation not compiled in\n");
  640. force_sig(SIGSEGV, current);
  641. return -ENOSYS;
  642. }
  643. #endif
  644. asmlinkage long sys_utrap_install(utrap_entry_t type,
  645. utrap_handler_t new_p,
  646. utrap_handler_t new_d,
  647. utrap_handler_t __user *old_p,
  648. utrap_handler_t __user *old_d)
  649. {
  650. if (type < UT_INSTRUCTION_EXCEPTION || type > UT_TRAP_INSTRUCTION_31)
  651. return -EINVAL;
  652. if (new_p == (utrap_handler_t)(long)UTH_NOCHANGE) {
  653. if (old_p) {
  654. if (!current_thread_info()->utraps) {
  655. if (put_user(NULL, old_p))
  656. return -EFAULT;
  657. } else {
  658. if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
  659. return -EFAULT;
  660. }
  661. }
  662. if (old_d) {
  663. if (put_user(NULL, old_d))
  664. return -EFAULT;
  665. }
  666. return 0;
  667. }
  668. if (!current_thread_info()->utraps) {
  669. current_thread_info()->utraps =
  670. kzalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long), GFP_KERNEL);
  671. if (!current_thread_info()->utraps)
  672. return -ENOMEM;
  673. current_thread_info()->utraps[0] = 1;
  674. } else {
  675. if ((utrap_handler_t)current_thread_info()->utraps[type] != new_p &&
  676. current_thread_info()->utraps[0] > 1) {
  677. long *p = current_thread_info()->utraps;
  678. current_thread_info()->utraps =
  679. kmalloc((UT_TRAP_INSTRUCTION_31+1)*sizeof(long),
  680. GFP_KERNEL);
  681. if (!current_thread_info()->utraps) {
  682. current_thread_info()->utraps = p;
  683. return -ENOMEM;
  684. }
  685. p[0]--;
  686. current_thread_info()->utraps[0] = 1;
  687. memcpy(current_thread_info()->utraps+1, p+1,
  688. UT_TRAP_INSTRUCTION_31*sizeof(long));
  689. }
  690. }
  691. if (old_p) {
  692. if (put_user((utrap_handler_t)(current_thread_info()->utraps[type]), old_p))
  693. return -EFAULT;
  694. }
  695. if (old_d) {
  696. if (put_user(NULL, old_d))
  697. return -EFAULT;
  698. }
  699. current_thread_info()->utraps[type] = (long)new_p;
  700. return 0;
  701. }
  702. long sparc_memory_ordering(unsigned long model, struct pt_regs *regs)
  703. {
  704. if (model >= 3)
  705. return -EINVAL;
  706. regs->tstate = (regs->tstate & ~TSTATE_MM) | (model << 14);
  707. return 0;
  708. }
  709. asmlinkage long sys_rt_sigaction(int sig,
  710. const struct sigaction __user *act,
  711. struct sigaction __user *oact,
  712. void __user *restorer,
  713. size_t sigsetsize)
  714. {
  715. struct k_sigaction new_ka, old_ka;
  716. int ret;
  717. /* XXX: Don't preclude handling different sized sigset_t's. */
  718. if (sigsetsize != sizeof(sigset_t))
  719. return -EINVAL;
  720. if (act) {
  721. new_ka.ka_restorer = restorer;
  722. if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
  723. return -EFAULT;
  724. }
  725. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  726. if (!ret && oact) {
  727. if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
  728. return -EFAULT;
  729. }
  730. return ret;
  731. }
  732. /* Invoked by rtrap code to update performance counters in
  733. * user space.
  734. */
  735. asmlinkage void update_perfctrs(void)
  736. {
  737. unsigned long pic, tmp;
  738. read_pic(pic);
  739. tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
  740. __put_user(tmp, current_thread_info()->user_cntd0);
  741. tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
  742. __put_user(tmp, current_thread_info()->user_cntd1);
  743. reset_pic();
  744. }
  745. asmlinkage long sys_perfctr(int opcode, unsigned long arg0, unsigned long arg1, unsigned long arg2)
  746. {
  747. int err = 0;
  748. switch(opcode) {
  749. case PERFCTR_ON:
  750. current_thread_info()->pcr_reg = arg2;
  751. current_thread_info()->user_cntd0 = (u64 __user *) arg0;
  752. current_thread_info()->user_cntd1 = (u64 __user *) arg1;
  753. current_thread_info()->kernel_cntd0 =
  754. current_thread_info()->kernel_cntd1 = 0;
  755. write_pcr(arg2);
  756. reset_pic();
  757. set_thread_flag(TIF_PERFCTR);
  758. break;
  759. case PERFCTR_OFF:
  760. err = -EINVAL;
  761. if (test_thread_flag(TIF_PERFCTR)) {
  762. current_thread_info()->user_cntd0 =
  763. current_thread_info()->user_cntd1 = NULL;
  764. current_thread_info()->pcr_reg = 0;
  765. write_pcr(0);
  766. clear_thread_flag(TIF_PERFCTR);
  767. err = 0;
  768. }
  769. break;
  770. case PERFCTR_READ: {
  771. unsigned long pic, tmp;
  772. if (!test_thread_flag(TIF_PERFCTR)) {
  773. err = -EINVAL;
  774. break;
  775. }
  776. read_pic(pic);
  777. tmp = (current_thread_info()->kernel_cntd0 += (unsigned int)pic);
  778. err |= __put_user(tmp, current_thread_info()->user_cntd0);
  779. tmp = (current_thread_info()->kernel_cntd1 += (pic >> 32));
  780. err |= __put_user(tmp, current_thread_info()->user_cntd1);
  781. reset_pic();
  782. break;
  783. }
  784. case PERFCTR_CLRPIC:
  785. if (!test_thread_flag(TIF_PERFCTR)) {
  786. err = -EINVAL;
  787. break;
  788. }
  789. current_thread_info()->kernel_cntd0 =
  790. current_thread_info()->kernel_cntd1 = 0;
  791. reset_pic();
  792. break;
  793. case PERFCTR_SETPCR: {
  794. u64 __user *user_pcr = (u64 __user *)arg0;
  795. if (!test_thread_flag(TIF_PERFCTR)) {
  796. err = -EINVAL;
  797. break;
  798. }
  799. err |= __get_user(current_thread_info()->pcr_reg, user_pcr);
  800. write_pcr(current_thread_info()->pcr_reg);
  801. current_thread_info()->kernel_cntd0 =
  802. current_thread_info()->kernel_cntd1 = 0;
  803. reset_pic();
  804. break;
  805. }
  806. case PERFCTR_GETPCR: {
  807. u64 __user *user_pcr = (u64 __user *)arg0;
  808. if (!test_thread_flag(TIF_PERFCTR)) {
  809. err = -EINVAL;
  810. break;
  811. }
  812. err |= __put_user(current_thread_info()->pcr_reg, user_pcr);
  813. break;
  814. }
  815. default:
  816. err = -EINVAL;
  817. break;
  818. };
  819. return err;
  820. }
  821. /*
  822. * Do a system call from kernel instead of calling sys_execve so we
  823. * end up with proper pt_regs.
  824. */
  825. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  826. {
  827. long __res;
  828. register long __g1 __asm__ ("g1") = __NR_execve;
  829. register long __o0 __asm__ ("o0") = (long)(filename);
  830. register long __o1 __asm__ ("o1") = (long)(argv);
  831. register long __o2 __asm__ ("o2") = (long)(envp);
  832. asm volatile ("t 0x6d\n\t"
  833. "sub %%g0, %%o0, %0\n\t"
  834. "movcc %%xcc, %%o0, %0\n\t"
  835. : "=r" (__res), "=&r" (__o0)
  836. : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__g1)
  837. : "cc");
  838. return __res;
  839. }