summaryrefslogtreecommitdiff
path: root/linux-user
AgeCommit message (Collapse)AuthorFilesLines
2011-04-12unicore32: add necessry headers in linux-user/unicore32 for unicore32 supportGuan Xuetao4-0/+454
Signed-off-by: Guan Xuetao <gxt@mprc.pku.edu.cn> Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
2011-04-11microblaze: Correct ec mask in debug printEdgar E. Iglesias1-1/+1
Signed-off-by: Edgar E. Iglesias <edgar.iglesias@gmail.com>
2011-03-22linux-user: Fix unlock_user() call in return from poll()Peter Maydell1-3/+1
Correct the broken attempt to calculate the third argument to unlock_user() in the code path which unlocked the pollfd array on return from poll() and ppoll() emulation. (This only caused a problem if unlock_user() wasn't a no-op, eg if DEBUG_REMAP is defined.) Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-03-06linux-user: Fix large seeks by 32 bit guest on 64 bit hostPeter Maydell1-6/+10
When emulating a 32 bit Linux user-mode program on a 64 bit target we implement the llseek syscall in terms of lseek. Correct a bug which meant we were silently casting the result of host lseek() to a 32 bit integer as it passed through get_errno() and thus throwing away the top half. We also don't try to store the result back to userspace unless the seek succeeded; this matches the kernel behaviour. Thanks to Eoghan Sherry for identifying the problem and suggesting a solution. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-03-03linux-user: fix compile failure if !CONFIG_USE_GUEST_BASEPeter Maydell1-0/+4
If CONFIG_USE_GUEST_BASE is not defined, gcc complains: linux-user/mmap.c:235: error: comparison of unsigned expression >= 0 is always true because RESERVED_VA is #defined to 0. Since mmap_find_vma_reserved() will never be called anyway if RESERVED_VA is always 0, fix this by simply #ifdef'ing away the function and its callsite. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-17linux-user: correct core dump formatLaurent Vivier1-16/+18
This patch allows to really use the core dumped by qemu with guest architecture tools. - it adds a missing bswap_phdr() for the program headers of memory regions. "objdump -x" sample: BEFORE: 0x1000000 off 0x00200000 vaddr 0x00000400 paddr 0x00000000 align 2**21 filesz 0x00000000 memsz 0x00100000 flags --- 0x1000000 off 0x00200000 vaddr 0x00100400 paddr 0x00000000 align 2**21 filesz 0x00000000 memsz 0x00080000 flags --- 6000000 AFTER: LOAD off 0x00002000 vaddr 0x00040000 paddr 0x00000000 align 2**13 filesz 0x00000000 memsz 0x00001000 flags --- LOAD off 0x00002000 vaddr 0x00041000 paddr 0x00000000 align 2**13 filesz 0x00000000 memsz 0x00000800 flags rw- - it doesn't pad the note size to sizeof(int32_t). On m68k the NT_PRSTATUS note size is 154 and must not be rounded up to 156, because this value is checked by objdump and gdb. "gdb" symptoms: "warning: Couldn't find general-purpose registers in core file." "objdump -x" sample: BEFORE: Sections: Idx Name Size VMA LMA File off Algn 0 note0 000001c4 00000000 00000000 000003b4 2**0 CONTENTS, READONLY 1 .auxv 00000070 00000000 00000000 00000508 2**2 CONTENTS 2 proc1 00100000 00000400 00000000 00200000 2**10 READONLY AFTER: Sections: Idx Name Size VMA LMA File off Algn 0 note0 000001c4 00000000 00000000 000003b4 2**0 CONTENTS, READONLY 1 .reg/19022 00000050 00000000 00000000 0000040e 2**2 CONTENTS 2 .reg 00000050 00000000 00000000 0000040e 2**2 CONTENTS 3 .auxv 00000070 00000000 00000000 00000508 2**2 CONTENTS 4 load1 00000000 00040000 00000000 00002000 2**13 ALLOC, READONLY Signed-off-by: Laurent Vivier <laurent@vivier.eu> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-17linux-user: Support the epoll syscallsPeter Maydell2-0/+120
Support the epoll family of syscalls: epoll_create(), epoll_create1(), epoll_ctl(), epoll_wait() and epoll_pwait(). Note that epoll_create1() and epoll_pwait() are later additions, so we have to test separately in configure for their presence. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-17linux-user: in linux-user/strace.c, tswap() is uselessLaurent Vivier1-46/+33
Syscall parameters are already swapped by the caller. This patch removes useless tswap() from strace.c $ QEMU_STRACE=1 chroot /m68k mknod myramdisk b 1 1 with tswap() ... 29944 mknod("myramdisk",026630200000) = 0 ... without tswap() ... 30042 mknod("myramdisk",S_IFBLK|0666,makedev(1,1)) = 0 ... natively: $ strace touch mytouch ... open("mytouch", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3 ... $ QEMU_STRACE=1 chroot /m68k touch mytouch with tswap() ... 30368 open("/usr/share/locale/locale.alias",O_RDONLY) = 3 30368 fstat64(50331648,0x4080032c) = 0 ... 30368 open("mytouch",O_RDONLY|O_CREAT|O_LARGEFILE|O_NOCTTY|O_NONBLOCK|0x1) = 0 ... without tswap() ... 30572 open("/usr/share/locale/locale.alias",O_RDONLY) = 3 30572 fstat64(3,0x4080032c) = 0 ... 30572 open("mytouch",O_WRONLY|O_CREAT|O_LARGEFILE|O_NOCTTY|O_NONBLOCK,0666) = 0 Signed-off-by: Laurent Vivier <laurent@vivier.eu> Fixes by Riku Voipio: add casts Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-17linux-user: add rmdir() straceLaurent Vivier2-0/+15
Signed-off-by: Laurent Vivier <laurent@vivier.eu> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-10linux-user/arm: fix compilation failures using softfloat's struct typesPeter Maydell3-19/+19
Add uses of the float32/float64 boxing and unboxing macros so that the ARM linux-user targets will compile with USE_SOFTFLOAT_STRUCT_TYPES enabled. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-02-09linux-user: fix for loopmount ioctlMartin Mohring1-2/+0
In case a chrooted build uses XEN or KVM, a looped mount needs to be done to setup the chroot. The ioctl for loop mount works correctly for arm, mips, ppc32 and sh4, so its now activated. Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-09linux-user: fix build errors for mmap2-only portsMike Frysinger1-1/+1
The current print_mmap func is only enabled when the target supports the mmap syscall, but both mmap and mmap2 syscalls use it. This leads to a build failure when the target supports mmap2 but not mmap. Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-09linux-user: implement sched_{g,s}etaffinityMike Frysinger1-0/+67
Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-09linux-user/FLAT: allow targets to override FLAT processingMike Frysinger2-16/+21
This brings flatload.c more in line with the current Linux FLAT loader which allows targets to handle various FLAT aspects in their own way. For the common behavior, the new functions get stubbed out. Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-09linux-user/FLAT: fix auto-stack sizingMike Frysinger1-2/+9
The current auto-stack sizing works like it does on a NOMMU system; the problem is that this only works if the envp/argv arrays are fairly slim. On a desktop system, this is rarely the case, and can easily blow past the stack and into data/text regions as the default stack for FLAT progs is a mere 4KiB. So rather than rely on the NOMMU calculation (which is only there because NOMMU can't easily allocate gobs of contiguous mem), calc the full space actually needed and let the MMU host make space. Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-09linux-user: decode MAP_{UNINITIALIZED,EXECUTABLE} in straceMike Frysinger2-0/+5
Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-09linux-user: add ppoll syscall supportMike Frysinger1-2/+55
Some architectures (like Blackfin) only implement ppoll (and skip poll). So add support for it using existing poll code. Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-09linux-user/elfload: add FDPIC supportMike Frysinger2-0/+78
Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-09linux-user: fix sizeof handling for getsockoptMike Frysinger1-2/+2
Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-09linux-user: Fix possible realloc memory leakStefan Weil1-3/+5
Extract from "man realloc": "If realloc() fails the original block is left untouched; it is not freed or moved." Fix a possible memory leak (reported by cppcheck). Cc: Riku Voipio <riku.voipio@iki.fi> Signed-off-by: Stefan Weil <weil@mail.berlios.de> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-09linux-user: Add support for -version optionPeter Maydell1-4/+13
Add support to the linux-user qemu for the -version command line option, bringing it into line with the system emulation qemu. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2011-02-01linux-user: avoid gcc array overrun warning for sparcPeter Maydell1-3/+4
Suppress a gcc array bounds overrun warning when filling in the SPARC signal frame by adjusting our definition of the structure so that the fp and callers_pc membes are part of the ins[] array rather than separate fields; since qemu has no need to access the fields individually there is no need to follow the kernel's structure field naming exactly. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
2011-01-23Delete useless 'extern' qualifiers for functionsBlue Swirl2-5/+5
'extern' qualifier is useless for function declarations. Delete them. Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
2011-01-14linux-user: ARM: clear the IT bits when invoking a signal handlerPeter Maydell1-7/+9
When invoking a signal handler for an ARM target, make sure the IT bits in the CPSR are cleared. (This would otherwise cause incorrect execution if the IT state was non-zero when an exception occured. This bug has been masked previously because we weren't getting the IT state bits at exception entry right anyway.) Also use the proper cpsr_read()/cpsr_write() interface to update the CPSR rather than manipulating CPUState fields directly. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Aurelien Jarno <aurelien@aurel32.net> Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-01-12linux-user: Add configure check for linux/fiemap.h and IOC_FS_FIEMAPPeter Maydell2-1/+5
Add a configure check for the existence of linux/fiemap.h and the IOC_FS_FIEMAP ioctl. This fixes a compilation failure on Linux systems which don't have that header file. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2011-01-07Remove dead code for ARM semihosting commandline handlingWolfgang Schildbach2-3/+0
There are some bits in the code which were used to store the commandline for the semihosting call. These bits are now write-only and can be removed. Signed-off-by: Wolfgang Schildbach <wschi@dolby.com> Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
2011-01-07linux-user: Fix incorrect NaN detection in ARM nwfpe emulationPeter Maydell1-7/+7
The code in the linux-user ARM nwfpe emulation was incorrectly checking only for quiet NaNs when it should have been checking for any kind of NaN. This is probably because the code in question was taken from the Linux kernel, whose copy of the softfloat library had been modified so that float*_is_nan() returned true for all NaNs, not just quiet ones. The qemu equivalent function is float*_is_any_nan(), so use that. NB that this code is really obsolete since nobody uses FPE for actual arithmetic now; this is just cleanup following the recent renaming of the NaN related functions. Acked-by: Aurelien Jarno <aurelien@aurel32.net> Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
2011-01-07linux-user: Implement FS_IOC_FIEMAP ioctlPeter Maydell4-0/+109
Implement the FS_IOC_FIEMAP ioctl using the new support for custom handling of ioctls; this is needed because the struct that is passed includes a variable-length array. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
2011-01-07linux-user: Support ioctls whose parameter size is not constantPeter Maydell1-3/+15
Some ioctls (for example FS_IOC_FIEMAP) use structures whose size is not constant. The generic argument conversion code in do_ioctl() cannot handle this, so add support for implementing a special-case handler for a particular ioctl which does the conversion itself. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
2011-01-07linux-user: Implement sync_file_range{,2} syscallsPeter Maydell2-0/+29
Implement the missing syscalls sync_file_range and sync_file_range2. The latter in particular is used by newer versions of apt on Ubuntu for ARM. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
2011-01-02softfloat: Rename float*_is_nan() functions to float*_is_quiet_nan()Peter Maydell1-7/+7
The softfloat functions float*_is_nan() were badly misnamed, because they return true only for quiet NaNs, not for all NaNs. Rename them to float*_is_quiet_nan() to more accurately reflect what they do. This change was produced by: perl -p -i -e 's/_is_nan/_is_quiet_nan/g' $(git grep -l is_nan) (with the results manually checked.) Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Nathan Froyd <froydnj@codesourcery.com> Acked-by: Edgar E. Iglesias <edgar.iglesias@gmail.com> Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
2010-12-07ARM: fix ldrexd/strexdPeter Maydell1-1/+1
Correct ldrexd and strexd code to always read and write the high word of the 64-bit value from addr+4. Also make ldrexd and strexd agree that for a 64 bit value the address in env->exclusive_addr is that of the low word. This fixes the issues reported in https://bugs.launchpad.net/qemu/+bug/670883 Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Nathan Froyd <froydnj@codesourcery.com>
2010-12-03linux-user: fix mips and ppc to use UID16Martin Mohring3-35/+35
Signed-off-by: Martin Mohring <martin.mohring@5edatasoft.com> Signed-off-by: Jan-Simon Möller <jsmoeller@linuxfoundation.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-12-03linux-user: fix compiler error on nptlRiku Voipio1-1/+2
Some compilers detect that new_stack isnt used after dd75d784 Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-12-03ARM: linux-user: Restore iWMMXT state from ucontext on sigreturnPeter Maydell1-0/+30
Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-12-03ARM: linux-user: Expose iWMMXT registers to signal handlersPeter Maydell1-0/+37
Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-12-03ARM: linux-user: Restore VFP state from ucontext on sigreturnPeter Maydell1-0/+40
Restore the VFP registers from the ucontext on return from a signal handler in linux-user mode. This means that signal handlers cannot accidentally corrupt the interrupted code's VFP state, and allows them to deliberately modify the state via the ucontext structure. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-12-03ARM: linux-user: Expose VFP registers to signal handlersPeter Maydell1-1/+46
For ARM linux-user mode signal handlers, fill in the ucontext with VFP register contents in the same way that the kernel does. We only do this for v2 format sigframe (2.6.12 and above); this is actually bug-for-bug compatible with the older kernels, which don't save and restore VFP registers either. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-12-03ARM: linux-user: Correct size of padding in target_ucontext_v2Peter Maydell1-1/+1
The padding in the target_ucontext_v2 is defined by the size of the target's sigset_t type, not the host's. (This bug only causes problems when we start using the uc_regspace[] array to expose VFP registers to userspace signal handlers.) Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-12-03linux-user: Translate getsockopt level optionJamie Lentin1-10/+61
n setsockopt, the socket level options are translated to the hosts' architecture before the real syscall is called, e.g. TARGET_SO_TYPE -> SO_TYPE. This patch does the same with getsockopt. Tested on a x86 host emulating MIPS. Without it:- $ grep getsockopt host.strace 31311 getsockopt(3, SOL_SOCKET, 0x1007 /* SO_??? */, 0xbff17208, 0xbff17204) = -1 ENOPROTOOPT (Protocol not available) With:- $ grep getsockopt host.strace 25706 getsockopt(3, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 Whitespace cleanup: Riku Voipio Signed-off-by: Jamie Lentin <jm@lentin.co.uk> Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
2010-12-03linux-user: remove unnecessary local from __get_user(), __put_user()Peter Maydell1-4/+2
Remove an unnecessary local variable from the __get_user() and __put_user() macros. This avoids confusing compilation failures if the name of the local variable ('size') happens to be the same as the variable the macro user is trying to read/write. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-12-03linux-user: fix memory leaks with NPTL emulationNathan Froyd3-8/+9
Running programs that create large numbers of threads, such as this snippet from libstdc++'s pthread7-rope.cc: const int max_thread_count = 4; const int max_loop_count = 10000; ... for (int j = 0; j < max_loop_count; j++) { ... for (int i = 0; i < max_thread_count; i++) pthread_create (&tid[i], NULL, thread_main, 0); for (int i = 0; i < max_thread_count; i++) pthread_join (tid[i], NULL); } in user-mode emulation will quickly run out of memory. This is caused by a failure to free memory in do_syscall prior to thread exit: /* TODO: Free CPU state. */ pthread_exit(NULL); The first step in fixing this is to make all TaskStates used by QEMU dynamically allocated. The TaskState used by the initial thread was not, as it was allocated on main's stack. So fix that, free the cpu_env, free the TaskState, and we're home free, right? Not exactly. When we create a thread, we do: ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE); ... new_stack = ts->stack; ... ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE); If we blindly free the TaskState, then, we yank the current (host) thread's stack out from underneath it while it still has things to do, like calling pthread_exit. That causes problems, as you might expect. The solution adopted here is to let the C library allocate the thread's stack (so the C library can properly clean it up at pthread_exit) and provide a hint that we want NEW_STACK_SIZE bytes of stack. With those two changes, we're done, right? Well, almost. You see, we're creating all these host threads and their parent threads never bother to check that their children are finished. There's no good place for the parent threads to do so. Therefore, we need to create the threads in a detached state so the parent thread doesn't have to call pthread_join on the child to release the child's resources; the child does so automatically. With those three major changes, we can comfortably run programs like the above without exhausting memory. We do need to delete 'stack' from the TaskState structure. Signed-off-by: Nathan Froyd <froydnj@codesourcery.com> Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-12-03linux-user: mmap_reserve() not controlled by RESERVED_VAamateur1-1/+3
mmap_reserve() should be called only when RESERVED_VA is enabled. Otherwise, unmaped virtual address space will never be reusable. This bug will exhaust virtual address space in extreme conditions. Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-11-23microblaze: target-ify target_ucontextRichard Henderson1-9/+9
Rename the members of target_ucontext so that they don't conflict with possible host macros for ucontext members. This has already been done for the other targets. Signed-off-by: Richard Henderson <rth@twiddle.net> Signed-off-by: Edgar E. Iglesias <edgar@axis.com>
2010-10-05linux-user: Fix typo m86k -> m68kStefan Weil1-3/+3
Replace m86k_sim_stat by m68k_sim_stat. Cc: Riku Voipio <riku.voipio@iki.fi> Signed-off-by: Stefan Weil <weil@mail.berlios.de>
2010-10-05Add new user mode option -ignore-environmentStefan Weil1-0/+6
An empty environment is sometimes useful in user mode. The new option provides it for linux-user and bsd-user (darwin-user still has no environment related options). The patch also adds the documentation for other environment related options. Signed-off-by: Stefan Weil <weil@mail.berlios.de>
2010-10-03Replace most gcc format attributes by macro GCC_FMT_ATTR (format checking)Stefan Weil1-1/+1
Since version 4.4.x, gcc supports additional format attributes. __attribute__ ((format (gnu_printf, 1, 2))) should be used instead of __attribute__ ((format (printf, 1, 2)) because QEMU always uses standard format strings (even with mingw32). The patch replaces format attribute printf / __printf__ by macro GCC_FMT_ATTR which uses gnu_printf if supported. It also removes an #ifdef __GNUC__ (not needed any longer). Cc: Blue Swirl <blauwirbel@gmail.com> Signed-off-by: Stefan Weil <weil@mail.berlios.de> Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
2010-09-18linux-user: improve flatload error checkingBlue Swirl1-1/+2
Because of the use of unsigned type, possible errors during load were ignored. Fix by using a signed type. This also avoids a warning with GCC flag -Wtype-limits. Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
2010-09-18linux-user: fix types in a comparisonBlue Swirl1-1/+1
-1ul is unsigned long, which does not necessarily match abi_ulong type. Fix by using abi_long instead. This also avoids a warning with GCC flag -Wtype-limits. Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
2010-09-18linux-user: fix socklen_t comparisonsBlue Swirl1-7/+13
On many systems, socklen_t is defined as unsigned. This means that checks for negative values are not meaningful. Fix by explicitly casting to a signed integer. This also avoids some warnings with GCC flag -Wtype-limits. Signed-off-by: Blue Swirl <blauwirbel@gmail.com>