xref: /linux/arch/x86/kernel/ldt.c (revision 57ad87ddce79b6d54f8e442d0ecf4b5bbe8c5a9e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
4  * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
5  * Copyright (C) 2002 Andi Kleen
6  *
7  * This handles calls from both 32bit and 64bit mode.
8  *
9  * Lock order:
10  *	contex.ldt_usr_sem
11  *	  mmap_sem
12  *	    context.lock
13  */
14 
15 #include <linux/errno.h>
16 #include <linux/gfp.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/smp.h>
21 #include <linux/syscalls.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/uaccess.h>
25 
26 #include <asm/ldt.h>
27 #include <asm/tlb.h>
28 #include <asm/desc.h>
29 #include <asm/mmu_context.h>
30 #include <asm/syscalls.h>
31 #include <asm/pgtable_areas.h>
32 
33 /* This is a multiple of PAGE_SIZE. */
34 #define LDT_SLOT_STRIDE (LDT_ENTRIES * LDT_ENTRY_SIZE)
35 
36 static inline void *ldt_slot_va(int slot)
37 {
38 	return (void *)(LDT_BASE_ADDR + LDT_SLOT_STRIDE * slot);
39 }
40 
41 void load_mm_ldt(struct mm_struct *mm)
42 {
43 	struct ldt_struct *ldt;
44 
45 	/* READ_ONCE synchronizes with smp_store_release */
46 	ldt = READ_ONCE(mm->context.ldt);
47 
48 	/*
49 	 * Any change to mm->context.ldt is followed by an IPI to all
50 	 * CPUs with the mm active.  The LDT will not be freed until
51 	 * after the IPI is handled by all such CPUs.  This means that,
52 	 * if the ldt_struct changes before we return, the values we see
53 	 * will be safe, and the new values will be loaded before we run
54 	 * any user code.
55 	 *
56 	 * NB: don't try to convert this to use RCU without extreme care.
57 	 * We would still need IRQs off, because we don't want to change
58 	 * the local LDT after an IPI loaded a newer value than the one
59 	 * that we can see.
60 	 */
61 
62 	if (unlikely(ldt)) {
63 		if (static_cpu_has(X86_FEATURE_PTI)) {
64 			if (WARN_ON_ONCE((unsigned long)ldt->slot > 1)) {
65 				/*
66 				 * Whoops -- either the new LDT isn't mapped
67 				 * (if slot == -1) or is mapped into a bogus
68 				 * slot (if slot > 1).
69 				 */
70 				clear_LDT();
71 				return;
72 			}
73 
74 			/*
75 			 * If page table isolation is enabled, ldt->entries
76 			 * will not be mapped in the userspace pagetables.
77 			 * Tell the CPU to access the LDT through the alias
78 			 * at ldt_slot_va(ldt->slot).
79 			 */
80 			set_ldt(ldt_slot_va(ldt->slot), ldt->nr_entries);
81 		} else {
82 			set_ldt(ldt->entries, ldt->nr_entries);
83 		}
84 	} else {
85 		clear_LDT();
86 	}
87 }
88 
89 void switch_ldt(struct mm_struct *prev, struct mm_struct *next)
90 {
91 	/*
92 	 * Load the LDT if either the old or new mm had an LDT.
93 	 *
94 	 * An mm will never go from having an LDT to not having an LDT.  Two
95 	 * mms never share an LDT, so we don't gain anything by checking to
96 	 * see whether the LDT changed.  There's also no guarantee that
97 	 * prev->context.ldt actually matches LDTR, but, if LDTR is non-NULL,
98 	 * then prev->context.ldt will also be non-NULL.
99 	 *
100 	 * If we really cared, we could optimize the case where prev == next
101 	 * and we're exiting lazy mode.  Most of the time, if this happens,
102 	 * we don't actually need to reload LDTR, but modify_ldt() is mostly
103 	 * used by legacy code and emulators where we don't need this level of
104 	 * performance.
105 	 *
106 	 * This uses | instead of || because it generates better code.
107 	 */
108 	if (unlikely((unsigned long)prev->context.ldt |
109 		     (unsigned long)next->context.ldt))
110 		load_mm_ldt(next);
111 
112 	DEBUG_LOCKS_WARN_ON(preemptible());
113 }
114 
115 static void refresh_ldt_segments(void)
116 {
117 #ifdef CONFIG_X86_64
118 	unsigned short sel;
119 
120 	/*
121 	 * Make sure that the cached DS and ES descriptors match the updated
122 	 * LDT.
123 	 */
124 	savesegment(ds, sel);
125 	if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
126 		loadsegment(ds, sel);
127 
128 	savesegment(es, sel);
129 	if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
130 		loadsegment(es, sel);
131 #endif
132 }
133 
134 /* context.lock is held by the task which issued the smp function call */
135 static void flush_ldt(void *__mm)
136 {
137 	struct mm_struct *mm = __mm;
138 
139 	if (this_cpu_read(cpu_tlbstate.loaded_mm) != mm)
140 		return;
141 
142 	load_mm_ldt(mm);
143 
144 	refresh_ldt_segments();
145 }
146 
147 /* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
148 static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
149 {
150 	struct ldt_struct *new_ldt;
151 	unsigned int alloc_size;
152 
153 	if (num_entries > LDT_ENTRIES)
154 		return NULL;
155 
156 	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
157 	if (!new_ldt)
158 		return NULL;
159 
160 	BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
161 	alloc_size = num_entries * LDT_ENTRY_SIZE;
162 
163 	/*
164 	 * Xen is very picky: it requires a page-aligned LDT that has no
165 	 * trailing nonzero bytes in any page that contains LDT descriptors.
166 	 * Keep it simple: zero the whole allocation and never allocate less
167 	 * than PAGE_SIZE.
168 	 */
169 	if (alloc_size > PAGE_SIZE)
170 		new_ldt->entries = vzalloc(alloc_size);
171 	else
172 		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
173 
174 	if (!new_ldt->entries) {
175 		kfree(new_ldt);
176 		return NULL;
177 	}
178 
179 	/* The new LDT isn't aliased for PTI yet. */
180 	new_ldt->slot = -1;
181 
182 	new_ldt->nr_entries = num_entries;
183 	return new_ldt;
184 }
185 
186 #ifdef CONFIG_PAGE_TABLE_ISOLATION
187 
188 static void do_sanity_check(struct mm_struct *mm,
189 			    bool had_kernel_mapping,
190 			    bool had_user_mapping)
191 {
192 	if (mm->context.ldt) {
193 		/*
194 		 * We already had an LDT.  The top-level entry should already
195 		 * have been allocated and synchronized with the usermode
196 		 * tables.
197 		 */
198 		WARN_ON(!had_kernel_mapping);
199 		if (boot_cpu_has(X86_FEATURE_PTI))
200 			WARN_ON(!had_user_mapping);
201 	} else {
202 		/*
203 		 * This is the first time we're mapping an LDT for this process.
204 		 * Sync the pgd to the usermode tables.
205 		 */
206 		WARN_ON(had_kernel_mapping);
207 		if (boot_cpu_has(X86_FEATURE_PTI))
208 			WARN_ON(had_user_mapping);
209 	}
210 }
211 
212 #ifdef CONFIG_X86_PAE
213 
214 static pmd_t *pgd_to_pmd_walk(pgd_t *pgd, unsigned long va)
215 {
216 	p4d_t *p4d;
217 	pud_t *pud;
218 
219 	if (pgd->pgd == 0)
220 		return NULL;
221 
222 	p4d = p4d_offset(pgd, va);
223 	if (p4d_none(*p4d))
224 		return NULL;
225 
226 	pud = pud_offset(p4d, va);
227 	if (pud_none(*pud))
228 		return NULL;
229 
230 	return pmd_offset(pud, va);
231 }
232 
233 static void map_ldt_struct_to_user(struct mm_struct *mm)
234 {
235 	pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
236 	pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
237 	pmd_t *k_pmd, *u_pmd;
238 
239 	k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
240 	u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
241 
242 	if (boot_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
243 		set_pmd(u_pmd, *k_pmd);
244 }
245 
246 static void sanity_check_ldt_mapping(struct mm_struct *mm)
247 {
248 	pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR);
249 	pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
250 	bool had_kernel, had_user;
251 	pmd_t *k_pmd, *u_pmd;
252 
253 	k_pmd      = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR);
254 	u_pmd      = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR);
255 	had_kernel = (k_pmd->pmd != 0);
256 	had_user   = (u_pmd->pmd != 0);
257 
258 	do_sanity_check(mm, had_kernel, had_user);
259 }
260 
261 #else /* !CONFIG_X86_PAE */
262 
263 static void map_ldt_struct_to_user(struct mm_struct *mm)
264 {
265 	pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR);
266 
267 	if (boot_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt)
268 		set_pgd(kernel_to_user_pgdp(pgd), *pgd);
269 }
270 
271 static void sanity_check_ldt_mapping(struct mm_struct *mm)
272 {
273 	pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR);
274 	bool had_kernel = (pgd->pgd != 0);
275 	bool had_user   = (kernel_to_user_pgdp(pgd)->pgd != 0);
276 
277 	do_sanity_check(mm, had_kernel, had_user);
278 }
279 
280 #endif /* CONFIG_X86_PAE */
281 
282 /*
283  * If PTI is enabled, this maps the LDT into the kernelmode and
284  * usermode tables for the given mm.
285  */
286 static int
287 map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
288 {
289 	unsigned long va;
290 	bool is_vmalloc;
291 	spinlock_t *ptl;
292 	int i, nr_pages;
293 
294 	if (!boot_cpu_has(X86_FEATURE_PTI))
295 		return 0;
296 
297 	/*
298 	 * Any given ldt_struct should have map_ldt_struct() called at most
299 	 * once.
300 	 */
301 	WARN_ON(ldt->slot != -1);
302 
303 	/* Check if the current mappings are sane */
304 	sanity_check_ldt_mapping(mm);
305 
306 	is_vmalloc = is_vmalloc_addr(ldt->entries);
307 
308 	nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);
309 
310 	for (i = 0; i < nr_pages; i++) {
311 		unsigned long offset = i << PAGE_SHIFT;
312 		const void *src = (char *)ldt->entries + offset;
313 		unsigned long pfn;
314 		pgprot_t pte_prot;
315 		pte_t pte, *ptep;
316 
317 		va = (unsigned long)ldt_slot_va(slot) + offset;
318 		pfn = is_vmalloc ? vmalloc_to_pfn(src) :
319 			page_to_pfn(virt_to_page(src));
320 		/*
321 		 * Treat the PTI LDT range as a *userspace* range.
322 		 * get_locked_pte() will allocate all needed pagetables
323 		 * and account for them in this mm.
324 		 */
325 		ptep = get_locked_pte(mm, va, &ptl);
326 		if (!ptep)
327 			return -ENOMEM;
328 		/*
329 		 * Map it RO so the easy to find address is not a primary
330 		 * target via some kernel interface which misses a
331 		 * permission check.
332 		 */
333 		pte_prot = __pgprot(__PAGE_KERNEL_RO & ~_PAGE_GLOBAL);
334 		/* Filter out unsuppored __PAGE_KERNEL* bits: */
335 		pgprot_val(pte_prot) &= __supported_pte_mask;
336 		pte = pfn_pte(pfn, pte_prot);
337 		set_pte_at(mm, va, ptep, pte);
338 		pte_unmap_unlock(ptep, ptl);
339 	}
340 
341 	/* Propagate LDT mapping to the user page-table */
342 	map_ldt_struct_to_user(mm);
343 
344 	ldt->slot = slot;
345 	return 0;
346 }
347 
348 static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
349 {
350 	unsigned long va;
351 	int i, nr_pages;
352 
353 	if (!ldt)
354 		return;
355 
356 	/* LDT map/unmap is only required for PTI */
357 	if (!boot_cpu_has(X86_FEATURE_PTI))
358 		return;
359 
360 	nr_pages = DIV_ROUND_UP(ldt->nr_entries * LDT_ENTRY_SIZE, PAGE_SIZE);
361 
362 	for (i = 0; i < nr_pages; i++) {
363 		unsigned long offset = i << PAGE_SHIFT;
364 		spinlock_t *ptl;
365 		pte_t *ptep;
366 
367 		va = (unsigned long)ldt_slot_va(ldt->slot) + offset;
368 		ptep = get_locked_pte(mm, va, &ptl);
369 		pte_clear(mm, va, ptep);
370 		pte_unmap_unlock(ptep, ptl);
371 	}
372 
373 	va = (unsigned long)ldt_slot_va(ldt->slot);
374 	flush_tlb_mm_range(mm, va, va + nr_pages * PAGE_SIZE, PAGE_SHIFT, false);
375 }
376 
377 #else /* !CONFIG_PAGE_TABLE_ISOLATION */
378 
379 static int
380 map_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt, int slot)
381 {
382 	return 0;
383 }
384 
385 static void unmap_ldt_struct(struct mm_struct *mm, struct ldt_struct *ldt)
386 {
387 }
388 #endif /* CONFIG_PAGE_TABLE_ISOLATION */
389 
390 static void free_ldt_pgtables(struct mm_struct *mm)
391 {
392 #ifdef CONFIG_PAGE_TABLE_ISOLATION
393 	struct mmu_gather tlb;
394 	unsigned long start = LDT_BASE_ADDR;
395 	unsigned long end = LDT_END_ADDR;
396 
397 	if (!boot_cpu_has(X86_FEATURE_PTI))
398 		return;
399 
400 	tlb_gather_mmu(&tlb, mm, start, end);
401 	free_pgd_range(&tlb, start, end, start, end);
402 	tlb_finish_mmu(&tlb, start, end);
403 #endif
404 }
405 
406 /* After calling this, the LDT is immutable. */
407 static void finalize_ldt_struct(struct ldt_struct *ldt)
408 {
409 	paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
410 }
411 
412 static void install_ldt(struct mm_struct *mm, struct ldt_struct *ldt)
413 {
414 	mutex_lock(&mm->context.lock);
415 
416 	/* Synchronizes with READ_ONCE in load_mm_ldt. */
417 	smp_store_release(&mm->context.ldt, ldt);
418 
419 	/* Activate the LDT for all CPUs using currents mm. */
420 	on_each_cpu_mask(mm_cpumask(mm), flush_ldt, mm, true);
421 
422 	mutex_unlock(&mm->context.lock);
423 }
424 
425 static void free_ldt_struct(struct ldt_struct *ldt)
426 {
427 	if (likely(!ldt))
428 		return;
429 
430 	paravirt_free_ldt(ldt->entries, ldt->nr_entries);
431 	if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
432 		vfree_atomic(ldt->entries);
433 	else
434 		free_page((unsigned long)ldt->entries);
435 	kfree(ldt);
436 }
437 
438 /*
439  * Called on fork from arch_dup_mmap(). Just copy the current LDT state,
440  * the new task is not running, so nothing can be installed.
441  */
442 int ldt_dup_context(struct mm_struct *old_mm, struct mm_struct *mm)
443 {
444 	struct ldt_struct *new_ldt;
445 	int retval = 0;
446 
447 	if (!old_mm)
448 		return 0;
449 
450 	mutex_lock(&old_mm->context.lock);
451 	if (!old_mm->context.ldt)
452 		goto out_unlock;
453 
454 	new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
455 	if (!new_ldt) {
456 		retval = -ENOMEM;
457 		goto out_unlock;
458 	}
459 
460 	memcpy(new_ldt->entries, old_mm->context.ldt->entries,
461 	       new_ldt->nr_entries * LDT_ENTRY_SIZE);
462 	finalize_ldt_struct(new_ldt);
463 
464 	retval = map_ldt_struct(mm, new_ldt, 0);
465 	if (retval) {
466 		free_ldt_pgtables(mm);
467 		free_ldt_struct(new_ldt);
468 		goto out_unlock;
469 	}
470 	mm->context.ldt = new_ldt;
471 
472 out_unlock:
473 	mutex_unlock(&old_mm->context.lock);
474 	return retval;
475 }
476 
477 /*
478  * No need to lock the MM as we are the last user
479  *
480  * 64bit: Don't touch the LDT register - we're already in the next thread.
481  */
482 void destroy_context_ldt(struct mm_struct *mm)
483 {
484 	free_ldt_struct(mm->context.ldt);
485 	mm->context.ldt = NULL;
486 }
487 
488 void ldt_arch_exit_mmap(struct mm_struct *mm)
489 {
490 	free_ldt_pgtables(mm);
491 }
492 
493 static int read_ldt(void __user *ptr, unsigned long bytecount)
494 {
495 	struct mm_struct *mm = current->mm;
496 	unsigned long entries_size;
497 	int retval;
498 
499 	down_read(&mm->context.ldt_usr_sem);
500 
501 	if (!mm->context.ldt) {
502 		retval = 0;
503 		goto out_unlock;
504 	}
505 
506 	if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
507 		bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
508 
509 	entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
510 	if (entries_size > bytecount)
511 		entries_size = bytecount;
512 
513 	if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
514 		retval = -EFAULT;
515 		goto out_unlock;
516 	}
517 
518 	if (entries_size != bytecount) {
519 		/* Zero-fill the rest and pretend we read bytecount bytes. */
520 		if (clear_user(ptr + entries_size, bytecount - entries_size)) {
521 			retval = -EFAULT;
522 			goto out_unlock;
523 		}
524 	}
525 	retval = bytecount;
526 
527 out_unlock:
528 	up_read(&mm->context.ldt_usr_sem);
529 	return retval;
530 }
531 
532 static int read_default_ldt(void __user *ptr, unsigned long bytecount)
533 {
534 	/* CHECKME: Can we use _one_ random number ? */
535 #ifdef CONFIG_X86_32
536 	unsigned long size = 5 * sizeof(struct desc_struct);
537 #else
538 	unsigned long size = 128;
539 #endif
540 	if (bytecount > size)
541 		bytecount = size;
542 	if (clear_user(ptr, bytecount))
543 		return -EFAULT;
544 	return bytecount;
545 }
546 
547 static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
548 {
549 	struct mm_struct *mm = current->mm;
550 	struct ldt_struct *new_ldt, *old_ldt;
551 	unsigned int old_nr_entries, new_nr_entries;
552 	struct user_desc ldt_info;
553 	struct desc_struct ldt;
554 	int error;
555 
556 	error = -EINVAL;
557 	if (bytecount != sizeof(ldt_info))
558 		goto out;
559 	error = -EFAULT;
560 	if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
561 		goto out;
562 
563 	error = -EINVAL;
564 	if (ldt_info.entry_number >= LDT_ENTRIES)
565 		goto out;
566 	if (ldt_info.contents == 3) {
567 		if (oldmode)
568 			goto out;
569 		if (ldt_info.seg_not_present == 0)
570 			goto out;
571 	}
572 
573 	if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
574 	    LDT_empty(&ldt_info)) {
575 		/* The user wants to clear the entry. */
576 		memset(&ldt, 0, sizeof(ldt));
577 	} else {
578 		if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
579 			error = -EINVAL;
580 			goto out;
581 		}
582 
583 		fill_ldt(&ldt, &ldt_info);
584 		if (oldmode)
585 			ldt.avl = 0;
586 	}
587 
588 	if (down_write_killable(&mm->context.ldt_usr_sem))
589 		return -EINTR;
590 
591 	old_ldt       = mm->context.ldt;
592 	old_nr_entries = old_ldt ? old_ldt->nr_entries : 0;
593 	new_nr_entries = max(ldt_info.entry_number + 1, old_nr_entries);
594 
595 	error = -ENOMEM;
596 	new_ldt = alloc_ldt_struct(new_nr_entries);
597 	if (!new_ldt)
598 		goto out_unlock;
599 
600 	if (old_ldt)
601 		memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);
602 
603 	new_ldt->entries[ldt_info.entry_number] = ldt;
604 	finalize_ldt_struct(new_ldt);
605 
606 	/*
607 	 * If we are using PTI, map the new LDT into the userspace pagetables.
608 	 * If there is already an LDT, use the other slot so that other CPUs
609 	 * will continue to use the old LDT until install_ldt() switches
610 	 * them over to the new LDT.
611 	 */
612 	error = map_ldt_struct(mm, new_ldt, old_ldt ? !old_ldt->slot : 0);
613 	if (error) {
614 		/*
615 		 * This only can fail for the first LDT setup. If an LDT is
616 		 * already installed then the PTE page is already
617 		 * populated. Mop up a half populated page table.
618 		 */
619 		if (!WARN_ON_ONCE(old_ldt))
620 			free_ldt_pgtables(mm);
621 		free_ldt_struct(new_ldt);
622 		goto out_unlock;
623 	}
624 
625 	install_ldt(mm, new_ldt);
626 	unmap_ldt_struct(mm, old_ldt);
627 	free_ldt_struct(old_ldt);
628 	error = 0;
629 
630 out_unlock:
631 	up_write(&mm->context.ldt_usr_sem);
632 out:
633 	return error;
634 }
635 
636 SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
637 		unsigned long , bytecount)
638 {
639 	int ret = -ENOSYS;
640 
641 	switch (func) {
642 	case 0:
643 		ret = read_ldt(ptr, bytecount);
644 		break;
645 	case 1:
646 		ret = write_ldt(ptr, bytecount, 1);
647 		break;
648 	case 2:
649 		ret = read_default_ldt(ptr, bytecount);
650 		break;
651 	case 0x11:
652 		ret = write_ldt(ptr, bytecount, 0);
653 		break;
654 	}
655 	/*
656 	 * The SYSCALL_DEFINE() macros give us an 'unsigned long'
657 	 * return type, but tht ABI for sys_modify_ldt() expects
658 	 * 'int'.  This cast gives us an int-sized value in %rax
659 	 * for the return code.  The 'unsigned' is necessary so
660 	 * the compiler does not try to sign-extend the negative
661 	 * return codes into the high half of the register when
662 	 * taking the value from int->long.
663 	 */
664 	return (unsigned int)ret;
665 }
666