Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions arch/lkl/include/uapi/asm/lkl_long.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef _ASM_UAPI_LKL_LONG_H
#define _ASM_UAPI_LKL_LONG_H

/*
* __lkl_long_t / __lkl_ulong_t: pointer-width integer types for the
Comment thread
xdqi marked this conversation as resolved.
Outdated
* LKL cross-world interface (host_ops, syscall dispatch, UAPI structs).
*
* These must be the same size on both sides of the kernel<->host boundary:
* - Kernel side (LP64 native GCC with -mabi=ms): long = 8 bytes
* - Host side (LLP64 MinGW-w64): long = 4 bytes, need long long
* - Host side (LP64 Linux/Cygwin/macOS): long = 8 bytes
*
* The headers_install.py script replaces all 'long' in generated user
* headers with these types to ensure ABI compatibility across data models.
*/
#if defined(_WIN64) && !defined(__LP64__)
/* LLP64: MinGW-w64 x64 user-space */
typedef long long __lkl_long_t;
typedef unsigned long long __lkl_ulong_t;
#else
/* LP64: kernel side, Linux, Cygwin, macOS, or any ILP32 system */
typedef long __lkl_long_t;
typedef unsigned long __lkl_ulong_t;
#endif

#endif /* _ASM_UAPI_LKL_LONG_H */
34 changes: 34 additions & 0 deletions arch/lkl/scripts/headers_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def lkl_prefix(self, w):
def install_headers(self):
self.find_headers("arch/lkl/include/uapi/asm/syscalls.h")
self.headers.add("arch/lkl/include/uapi/asm/host_ops.h")
self.headers.add("arch/lkl/include/uapi/asm/lkl_long.h")
self.find_headers("include/uapi/linux/android/binder.h")
self.find_headers("include/uapi/linux/uhid.h")
self.find_headers("include/uapi/linux/mman.h")
Expand Down Expand Up @@ -160,6 +161,37 @@ def find_all_symbols(self):
# needed for i386
self.defines.add("__NR_stime")


def replace_long_types(self, content, h):
"""Replace 'long' and 'unsigned long' with __lkl_long_t/__lkl_ulong_t
for LLP64 compatibility, preserving 'long long' and 'long double'."""
# Skip lkl_long.h itself
if 'lkl_long.h' in h:
return content
# Step 1: protect 'long long' and 'long double' with placeholders
content = re.sub(r'\blong\s+long\b', '__LKLPH_LONGLONG__', content)
content = re.sub(r'\blong\s+double\b', '__LKLPH_LONGDOUBLE__', content)
# Step 2: replace 'unsigned long [int]' (now safe, no 'long long' present)
content = re.sub(r'\bunsigned\s+long(?:\s+int)?\b', '__lkl_ulong_t', content)
# Step 3: replace remaining 'long [int]'
content = re.sub(r'\blong(?:\s+int)?\b', '__lkl_long_t', content)
# Step 4: restore placeholders
content = content.replace('__LKLPH_LONGLONG__', 'long long')
content = content.replace('__LKLPH_LONGDOUBLE__', 'long double')
# Step 5: add include for __lkl_long_t definition if replacements were made
if '__lkl_long_t' in content or '__lkl_ulong_t' in content:
# Insert after the first #ifndef/#define guard or at the top
include_line = '#include <lkl/asm/lkl_long.h>\n'
if include_line not in content:
# Insert after the header guard #define
m = re.search(r'(#ifndef\s+\w+\s*\n#define\s+\w+\s*\n)', content)
if m:
pos = m.end()
content = content[:pos] + '\n' + include_line + '\n' + content[pos:]
else:
content = include_line + content
return content

def update_header(self, h):
print(" REPLACE\t%s" % h)
content = open(h).read()
Expand All @@ -184,6 +216,8 @@ def update_header(self, h):
search_str = r"(\W?union\s+)" + s + r"(\W)"
replace_str = "\\1" + self.lkl_prefix(s) + "\\2"
content = re.sub(search_str, replace_str, content, flags = re.MULTILINE)
# Replace long/unsigned long with __lkl_long_t/__lkl_ulong_t
content = self.replace_long_types(content, h)
open(h, 'w').write(content)

def update_headers(self):
Expand Down
2 changes: 2 additions & 0 deletions tools/lkl/Makefile.autoconf
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ endef

define nt_host
$(call set_autoconf_var,NT,y)
$(call set_kernel_config,INIT_STACK_ALL_ZERO,n)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used to make my old Cygwin GCC 11 happy. We can remove it if we maintain our own newer Cygwin cross compiler.

KOPT = "KALLSYMS_EXTRA_PASS=1"
KOPT += "HOSTCFLAGS=-Wno-char-subscripts"
KOPT += "HOSTLDFLAGS=-s"
LDLIBS += -lws2_32
LDFLAGS += -Wl,--image-base,0x10000
EXESUF := .exe
SOSUF := .dll
CFLAGS += -Iinclude/mingw32
Expand Down
1 change: 1 addition & 0 deletions tools/lkl/bin/x86_64-w64-mingw32-cc
22 changes: 22 additions & 0 deletions tools/lkl/bin/x86_64-w64-mingw32-gcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
# Shim: routes kernel code to cygwin cross-gcc, user-space to mingw-gcc.
# Cygwin-gcc: LP64 (long=64-bit), direct PE/COFF output, no Wine needed.
# Mingw-gcc: LLP64 (long=32-bit), standard Win64 libraries.
# The LKL API header uses __lkl_long_t (always 64-bit) to bridge the difference.
REAL_MINGW_GCC="/usr/bin/x86_64-w64-mingw32-gcc"
CYGWIN_GCC="/usr/bin/x86_64-pc-cygwin-gcc"
LKL_BIN_DIR="$(cd "$(dirname "$0")" && pwd)"

# Kernel code → cygwin-gcc (LP64)
for arg in "$@"; do
if [[ "$arg" == "-D__KERNEL__" ]]; then
exec "$CYGWIN_GCC" "$@"
fi
done

# User-space: use mingw-gcc with patched ld via specs
SPECS="$LKL_BIN_DIR/.lkl-linker.specs"
if [[ ! -f "$SPECS" ]]; then
printf '*linker:\n%s/x86_64-w64-mingw32-ld\n' "$LKL_BIN_DIR" > "$SPECS"
fi
exec "$REAL_MINGW_GCC" -specs="$SPECS" "$@"
Binary file added tools/lkl/bin/x86_64-w64-mingw32-ld
Comment thread
xdqi marked this conversation as resolved.
Binary file not shown.
Binary file added tools/lkl/bin/x86_64-w64-mingw32-objcopy
Binary file not shown.
60 changes: 30 additions & 30 deletions tools/lkl/include/lkl.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static inline int lkl_sys_fstatfs(unsigned int fd, struct lkl_statfs *buf)
static inline int lkl_sys_nanosleep(struct __lkl__kernel_timespec *rqtp,
struct __lkl__kernel_timespec *rmtp)
{
long p[6] = {LKL_CLOCK_MONOTONIC, 0, (long)rqtp, (long)rmtp, 0};
__lkl_long_t p[6] = {LKL_CLOCK_MONOTONIC, 0, (__lkl_long_t)rqtp, (__lkl_long_t)rmtp, 0};

return lkl_syscall(__lkl__NR_clock_nanosleep_time64, p);
}
Expand All @@ -93,7 +93,7 @@ static inline long long lkl_sys_lseek(unsigned int fd, __lkl__kernel_loff_t off,
unsigned int whence)
{
long long res;
long ret = lkl_sys_llseek(fd, off >> 32, off & 0xffffffff, &res, whence);
__lkl_long_t ret = lkl_sys_llseek(fd, off >> 32, off & 0xffffffff, &res, whence);

return ret < 0 ? ret : res;
}
Expand All @@ -102,7 +102,7 @@ static inline long long lkl_sys_lseek(unsigned int fd, __lkl__kernel_loff_t off,
static inline void *lkl_sys_mmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset)
{
return (void *)lkl_sys_mmap_pgoff((long)addr, length, prot, flags, fd,
return (void *)lkl_sys_mmap_pgoff((__lkl_long_t)addr, length, prot, flags, fd,
offset >> 12);
}

Expand All @@ -112,15 +112,15 @@ static inline void *lkl_sys_mmap(void *addr, size_t length, int prot, int flags,
/**
* lkl_sys_open - wrapper for lkl_sys_openat
*/
static inline long lkl_sys_open(const char *file, int flags, int mode)
static inline __lkl_long_t lkl_sys_open(const char *file, int flags, int mode)
{
return lkl_sys_openat(LKL_AT_FDCWD, file, flags, mode);
}

/**
* lkl_sys_creat - wrapper for lkl_sys_openat
*/
static inline long lkl_sys_creat(const char *file, int mode)
static inline __lkl_long_t lkl_sys_creat(const char *file, int mode)
{
return lkl_sys_openat(LKL_AT_FDCWD, file,
LKL_O_CREAT|LKL_O_WRONLY|LKL_O_TRUNC, mode);
Expand All @@ -132,7 +132,7 @@ static inline long lkl_sys_creat(const char *file, int mode)
/**
* lkl_sys_access - wrapper for lkl_sys_faccessat
*/
static inline long lkl_sys_access(const char *file, int mode)
static inline __lkl_long_t lkl_sys_access(const char *file, int mode)
{
return lkl_sys_faccessat(LKL_AT_FDCWD, file, mode);
}
Expand All @@ -142,7 +142,7 @@ static inline long lkl_sys_access(const char *file, int mode)
/**
* lkl_sys_chown - wrapper for lkl_sys_fchownat
*/
static inline long lkl_sys_chown(const char *path, lkl_uid_t uid, lkl_gid_t gid)
static inline __lkl_long_t lkl_sys_chown(const char *path, lkl_uid_t uid, lkl_gid_t gid)
{
return lkl_sys_fchownat(LKL_AT_FDCWD, path, uid, gid, 0);
}
Expand All @@ -152,7 +152,7 @@ static inline long lkl_sys_chown(const char *path, lkl_uid_t uid, lkl_gid_t gid)
/**
* lkl_sys_chmod - wrapper for lkl_sys_fchmodat
*/
static inline long lkl_sys_chmod(const char *path, mode_t mode)
static inline __lkl_long_t lkl_sys_chmod(const char *path, mode_t mode)
{
return lkl_sys_fchmodat(LKL_AT_FDCWD, path, mode);
}
Expand All @@ -162,7 +162,7 @@ static inline long lkl_sys_chmod(const char *path, mode_t mode)
/**
* lkl_sys_link - wrapper for lkl_sys_linkat
*/
static inline long lkl_sys_link(const char *existing, const char *new)
static inline __lkl_long_t lkl_sys_link(const char *existing, const char *new)
{
return lkl_sys_linkat(LKL_AT_FDCWD, existing, LKL_AT_FDCWD, new, 0);
}
Expand All @@ -172,7 +172,7 @@ static inline long lkl_sys_link(const char *existing, const char *new)
/**
* lkl_sys_unlink - wrapper for lkl_sys_unlinkat
*/
static inline long lkl_sys_unlink(const char *path)
static inline __lkl_long_t lkl_sys_unlink(const char *path)
{
return lkl_sys_unlinkat(LKL_AT_FDCWD, path, 0);
}
Expand All @@ -182,7 +182,7 @@ static inline long lkl_sys_unlink(const char *path)
/**
* lkl_sys_symlink - wrapper for lkl_sys_symlinkat
*/
static inline long lkl_sys_symlink(const char *existing, const char *new)
static inline __lkl_long_t lkl_sys_symlink(const char *existing, const char *new)
{
return lkl_sys_symlinkat(existing, LKL_AT_FDCWD, new);
}
Expand All @@ -192,7 +192,7 @@ static inline long lkl_sys_symlink(const char *existing, const char *new)
/**
* lkl_sys_readlink - wrapper for lkl_sys_readlinkat
*/
static inline long lkl_sys_readlink(const char *path, char *buf, size_t bufsize)
static inline __lkl_long_t lkl_sys_readlink(const char *path, char *buf, size_t bufsize)
{
return lkl_sys_readlinkat(LKL_AT_FDCWD, path, buf, bufsize);
}
Expand All @@ -202,7 +202,7 @@ static inline long lkl_sys_readlink(const char *path, char *buf, size_t bufsize)
/**
* lkl_sys_rename - wrapper for lkl_sys_renameat
*/
static inline long lkl_sys_rename(const char *old, const char *new)
static inline __lkl_long_t lkl_sys_rename(const char *old, const char *new)
{
return lkl_sys_renameat(LKL_AT_FDCWD, old, LKL_AT_FDCWD, new);
}
Expand All @@ -212,7 +212,7 @@ static inline long lkl_sys_rename(const char *old, const char *new)
/**
* lkl_sys_mkdir - wrapper for lkl_sys_mkdirat
*/
static inline long lkl_sys_mkdir(const char *path, mode_t mode)
static inline __lkl_long_t lkl_sys_mkdir(const char *path, mode_t mode)
{
return lkl_sys_mkdirat(LKL_AT_FDCWD, path, mode);
}
Expand All @@ -222,7 +222,7 @@ static inline long lkl_sys_mkdir(const char *path, mode_t mode)
/**
* lkl_sys_rmdir - wrapper for lkl_sys_unlinkrat
*/
static inline long lkl_sys_rmdir(const char *path)
static inline __lkl_long_t lkl_sys_rmdir(const char *path)
{
return lkl_sys_unlinkat(LKL_AT_FDCWD, path, LKL_AT_REMOVEDIR);
}
Expand All @@ -232,7 +232,7 @@ static inline long lkl_sys_rmdir(const char *path)
/**
* lkl_sys_mknod - wrapper for lkl_sys_mknodat
*/
static inline long lkl_sys_mknod(const char *path, mode_t mode, dev_t dev)
static inline __lkl_long_t lkl_sys_mknod(const char *path, mode_t mode, dev_t dev)
{
return lkl_sys_mknodat(LKL_AT_FDCWD, path, mode, dev);
}
Expand All @@ -242,7 +242,7 @@ static inline long lkl_sys_mknod(const char *path, mode_t mode, dev_t dev)
/**
* lkl_sys_pipe - wrapper for lkl_sys_pipe2
*/
static inline long lkl_sys_pipe(int fd[2])
static inline __lkl_long_t lkl_sys_pipe(int fd[2])
{
return lkl_sys_pipe2(fd, 0);
}
Expand All @@ -252,7 +252,7 @@ static inline long lkl_sys_pipe(int fd[2])
/**
* lkl_sys_send - wrapper for lkl_sys_sendto
*/
static inline long lkl_sys_send(int fd, void *buf, size_t len, int flags)
static inline __lkl_long_t lkl_sys_send(int fd, void *buf, size_t len, int flags)
{
return lkl_sys_sendto(fd, buf, len, flags, 0, 0);
}
Expand All @@ -262,7 +262,7 @@ static inline long lkl_sys_send(int fd, void *buf, size_t len, int flags)
/**
* lkl_sys_recv - wrapper for lkl_sys_recvfrom
*/
static inline long lkl_sys_recv(int fd, void *buf, size_t len, int flags)
static inline __lkl_long_t lkl_sys_recv(int fd, void *buf, size_t len, int flags)
{
return lkl_sys_recvfrom(fd, buf, len, flags, 0, 0);
}
Expand All @@ -272,10 +272,10 @@ static inline long lkl_sys_recv(int fd, void *buf, size_t len, int flags)
/**
* lkl_sys_select - wrapper for lkl_sys_pselect
*/
static inline long lkl_sys_select(int n, lkl_fd_set *rfds, lkl_fd_set *wfds,
static inline __lkl_long_t lkl_sys_select(int n, lkl_fd_set *rfds, lkl_fd_set *wfds,
lkl_fd_set *efds, struct lkl_timeval *tv)
{
long data[2] = { 0, _LKL_NSIG/8 };
__lkl_long_t data[2] = { 0, _LKL_NSIG/8 };
struct __lkl__kernel_timespec ts;

if (tv) {
Expand All @@ -293,7 +293,7 @@ static inline long lkl_sys_select(int n, lkl_fd_set *rfds, lkl_fd_set *wfds,
/**
* lkl_sys_poll - wrapper for lkl_sys_ppoll
*/
static inline long lkl_sys_poll(struct lkl_pollfd *fds, int n, int timeout)
static inline __lkl_long_t lkl_sys_poll(struct lkl_pollfd *fds, int n, int timeout)
{
struct __lkl__kernel_timespec ts;

Expand All @@ -311,7 +311,7 @@ static inline long lkl_sys_poll(struct lkl_pollfd *fds, int n, int timeout)
/**
* lkl_sys_epoll_create - wrapper for lkl_sys_epoll_create1
*/
static inline long lkl_sys_epoll_create(int size)
static inline __lkl_long_t lkl_sys_epoll_create(int size)
{
return lkl_sys_epoll_create1(0);
}
Expand All @@ -321,7 +321,7 @@ static inline long lkl_sys_epoll_create(int size)
/**
* lkl_sys_epoll_wait - wrapper for lkl_sys_epoll_pwait
*/
static inline long lkl_sys_epoll_wait(int fd, struct lkl_epoll_event *ev,
static inline __lkl_long_t lkl_sys_epoll_wait(int fd, struct lkl_epoll_event *ev,
int cnt, int to)
{
return lkl_sys_epoll_pwait(fd, ev, cnt, to, 0, _LKL_NSIG/8);
Expand Down Expand Up @@ -428,7 +428,7 @@ int lkl_get_virtio_blkdev(int disk_id, unsigned int part, uint32_t *pdevid);
* @mnt_str_len - size of mnt_str
* @returns - 0 on success, a negative value on error
*/
long lkl_mount_dev(unsigned int disk_id, unsigned int part, const char *fs_type,
__lkl_long_t lkl_mount_dev(unsigned int disk_id, unsigned int part, const char *fs_type,
int flags, const char *opts,
char *mnt_str, unsigned int mnt_str_len);

Expand All @@ -447,7 +447,7 @@ long lkl_mount_dev(unsigned int disk_id, unsigned int part, const char *fs_type,
* @mnt_str_len - size of mnt_str
* @returns - 0 on success, a negative value on error
*/
long lkl_mount_blkdev(unsigned int dev, const char *fs_type, int flags,
__lkl_long_t lkl_mount_blkdev(unsigned int dev, const char *fs_type, int flags,
const char *opts, char *mnt_str,
unsigned int mnt_str_len);

Expand All @@ -464,8 +464,8 @@ long lkl_mount_blkdev(unsigned int dev, const char *fs_type, int flags,
* umount can succeed
* @returns - 0 on success, a negative value on error
*/
long lkl_umount_dev(unsigned int disk_id, unsigned int part, int flags,
long timeout_ms);
__lkl_long_t lkl_umount_dev(unsigned int disk_id, unsigned int part, int flags,
__lkl_long_t timeout_ms);

/**
* lkl_umount_blkdev - umount a block device
Expand All @@ -478,7 +478,7 @@ long lkl_umount_dev(unsigned int disk_id, unsigned int part, int flags,
* umount can succeed
* @returns - 0 on success, a negative value on error
*/
long lkl_umount_blkdev(unsigned int dev, int flags, long timeout_ms);
__lkl_long_t lkl_umount_blkdev(unsigned int dev, int flags, __lkl_long_t timeout_ms);

/**
* lkl_umount_timeout - umount filesystem with timeout
Expand All @@ -489,7 +489,7 @@ long lkl_umount_blkdev(unsigned int dev, int flags, long timeout_ms);
* umount can succeed
* @returns - 0 on success, a negative value on error
*/
long lkl_umount_timeout(char *path, int flags, long timeout_ms);
__lkl_long_t lkl_umount_timeout(char *path, int flags, __lkl_long_t timeout_ms);

/**
* lkl_opendir - open a directory
Expand Down
Loading
Loading