Revert "arch/sim: avoid host-call being interrupted before getting errno"

This reverts commit ac5b38c9e5.

Keep host_errno_convert as a common interface in sim_internal.h
Keep the up_irq_save & up_irq_restore as common interface

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
This commit is contained in:
buxiasen
2025-07-29 22:55:34 +08:00
committed by Xiang Xiao
parent 88c048b48b
commit 8d2ef7f43c
4 changed files with 157 additions and 73 deletions
+118 -29
View File
@@ -194,7 +194,13 @@ int host_open(const char *pathname, int flags, int mode)
mapflags |= O_DIRECTORY; mapflags |= O_DIRECTORY;
} }
return host_uninterruptible_errno(open, pathname, mapflags, mode); int ret = open(pathname, mapflags, mode);
if (ret == -1)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -205,7 +211,13 @@ int host_close(int fd)
{ {
/* Just call the close routine */ /* Just call the close routine */
return host_uninterruptible_errno(close, fd); int ret = close(fd);
if (ret == -1)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -216,7 +228,13 @@ nuttx_ssize_t host_read(int fd, void *buf, nuttx_size_t count)
{ {
/* Just call the read routine */ /* Just call the read routine */
return host_uninterruptible_errno(read, fd, buf, count); nuttx_ssize_t ret = read(fd, buf, count);
if (ret == -1)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -227,7 +245,13 @@ nuttx_ssize_t host_write(int fd, const void *buf, nuttx_size_t count)
{ {
/* Just call the write routine */ /* Just call the write routine */
return host_uninterruptible_errno(write, fd, buf, count); nuttx_ssize_t ret = write(fd, buf, count);
if (ret == -1)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -239,7 +263,13 @@ nuttx_off_t host_lseek(int fd, nuttx_off_t pos, nuttx_off_t offset,
{ {
/* Just call the lseek routine */ /* Just call the lseek routine */
return host_uninterruptible_errno(lseek, fd, offset, whence); nuttx_off_t ret = lseek(fd, offset, whence);
if (ret == (nuttx_off_t)-1)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -250,7 +280,13 @@ int host_ioctl(int fd, int request, unsigned long arg)
{ {
/* Just call the ioctl routine */ /* Just call the ioctl routine */
return host_uninterruptible_errno(ioctl, fd, request, arg); int ret = ioctl(fd, request, arg);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -270,7 +306,13 @@ void host_sync(int fd)
int host_dup(int fd) int host_dup(int fd)
{ {
return host_uninterruptible_errno(dup, fd); int ret = dup(fd);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -284,7 +326,11 @@ int host_fstat(int fd, struct nuttx_stat_s *buf)
/* Call the host's stat routine */ /* Call the host's stat routine */
ret = host_uninterruptible_errno(fstat, fd, &hostbuf); ret = fstat(fd, &hostbuf);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
/* Map the return values */ /* Map the return values */
@@ -303,19 +349,19 @@ int host_fchstat(int fd, const struct nuttx_stat_s *buf, int flags)
if (flags & NUTTX_CH_STAT_MODE) if (flags & NUTTX_CH_STAT_MODE)
{ {
ret = host_uninterruptible_errno(fchmod, fd, buf->st_mode); ret = fchmod(fd, buf->st_mode);
if (ret < 0) if (ret < 0)
{ {
return ret; return host_errno_convert(-errno);
} }
} }
if (flags & (NUTTX_CH_STAT_UID | NUTTX_CH_STAT_GID)) if (flags & (NUTTX_CH_STAT_UID | NUTTX_CH_STAT_GID))
{ {
ret = host_uninterruptible_errno(fchown, fd, buf->st_uid, buf->st_gid); ret = fchown(fd, buf->st_uid, buf->st_gid);
if (ret < 0) if (ret < 0)
{ {
return ret; return host_errno_convert(-errno);
} }
} }
@@ -343,10 +389,10 @@ int host_fchstat(int fd, const struct nuttx_stat_s *buf, int flags)
times[1].tv_nsec = UTIME_OMIT; times[1].tv_nsec = UTIME_OMIT;
} }
ret = host_uninterruptible_errno(futimens, fd, times); ret = futimens(fd, times);
if (ret < 0) if (ret < 0)
{ {
return ret; return host_errno_convert(-errno);
} }
} }
@@ -359,7 +405,13 @@ int host_fchstat(int fd, const struct nuttx_stat_s *buf, int flags)
int host_ftruncate(int fd, nuttx_off_t length) int host_ftruncate(int fd, nuttx_off_t length)
{ {
return host_uninterruptible_errno(ftruncate, fd, length); int ret = ftruncate(fd, length);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -449,7 +501,13 @@ void host_rewinddir(void *dirp)
int host_closedir(void *dirp) int host_closedir(void *dirp)
{ {
return host_uninterruptible_errno(closedir, dirp); int ret = closedir(dirp);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -463,7 +521,11 @@ int host_statfs(const char *path, struct nuttx_statfs_s *buf)
/* Call the host's statfs routine */ /* Call the host's statfs routine */
ret = host_uninterruptible_errno(statvfs, path, &hostbuf); ret = statvfs(path, &hostbuf);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
/* Map the struct statfs value */ /* Map the struct statfs value */
@@ -485,7 +547,13 @@ int host_statfs(const char *path, struct nuttx_statfs_s *buf)
int host_unlink(const char *pathname) int host_unlink(const char *pathname)
{ {
return host_uninterruptible_errno(unlink, pathname); int ret = unlink(pathname);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -496,7 +564,13 @@ int host_mkdir(const char *pathname, int mode)
{ {
/* Just call the host's mkdir routine */ /* Just call the host's mkdir routine */
return host_uninterruptible_errno(mkdir, pathname, mode); int ret = mkdir(pathname, mode);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -505,7 +579,13 @@ int host_mkdir(const char *pathname, int mode)
int host_rmdir(const char *pathname) int host_rmdir(const char *pathname)
{ {
return host_uninterruptible_errno(rmdir, pathname); int ret = rmdir(pathname);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -514,7 +594,13 @@ int host_rmdir(const char *pathname)
int host_rename(const char *oldpath, const char *newpath) int host_rename(const char *oldpath, const char *newpath)
{ {
return host_uninterruptible_errno(rename, oldpath, newpath); int ret = rename(oldpath, newpath);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@@ -528,7 +614,11 @@ int host_stat(const char *path, struct nuttx_stat_s *buf)
/* Call the host's stat routine */ /* Call the host's stat routine */
ret = host_uninterruptible_errno(stat, path, &hostbuf); ret = stat(path, &hostbuf);
if (ret < 0)
{
ret = host_errno_convert(-errno);
}
/* Map the return values */ /* Map the return values */
@@ -547,20 +637,19 @@ int host_chstat(const char *path, const struct nuttx_stat_s *buf, int flags)
if (flags & NUTTX_CH_STAT_MODE) if (flags & NUTTX_CH_STAT_MODE)
{ {
ret = host_uninterruptible_errno(chmod, path, buf->st_mode); ret = chmod(path, buf->st_mode);
if (ret < 0) if (ret < 0)
{ {
return ret; return host_errno_convert(-errno);
} }
} }
if (flags & (NUTTX_CH_STAT_UID | NUTTX_CH_STAT_GID)) if (flags & (NUTTX_CH_STAT_UID | NUTTX_CH_STAT_GID))
{ {
ret = host_uninterruptible_errno(chown, path, ret = chown(path, buf->st_uid, buf->st_gid);
buf->st_uid, buf->st_gid);
if (ret < 0) if (ret < 0)
{ {
return ret; return host_errno_convert(-errno);
} }
} }
@@ -588,10 +677,10 @@ int host_chstat(const char *path, const struct nuttx_stat_s *buf, int flags)
times[1].tv_nsec = UTIME_OMIT; times[1].tv_nsec = UTIME_OMIT;
} }
ret = host_uninterruptible_errno(utimensat, AT_FDCWD, path, times, 0); ret = utimensat(AT_FDCWD, path, times, 0);
if (ret < 0) if (ret < 0)
{ {
return ret; return host_errno_convert(-errno);
} }
} }
+8 -14
View File
@@ -128,38 +128,32 @@ int host_system(char *buf, size_t len, const char *fmt, ...)
int ret; int ret;
char cmd[512]; char cmd[512];
va_list vars; va_list vars;
uint64_t flags;
va_start(vars, fmt); va_start(vars, fmt);
ret = host_uninterruptible_errno(vsnprintf, cmd, sizeof(cmd), fmt, vars); ret = vsnprintf(cmd, sizeof(cmd), fmt, vars);
va_end(vars); va_end(vars);
if (ret <= 0 || ret > sizeof(cmd)) if (ret <= 0 || ret > sizeof(cmd))
{ {
return ret < 0 ? ret : -EINVAL; return ret < 0 ? -errno : -EINVAL;
} }
if (buf == NULL) if (buf == NULL)
{ {
ret = host_uninterruptible_errno(system, cmd); ret = host_uninterruptible(system, cmd);
} }
else else
{ {
flags = up_irq_save();
fp = host_uninterruptible(popen, cmd, "r"); fp = host_uninterruptible(popen, cmd, "r");
if (fp == NULL) if (fp == NULL)
{ {
ret = -errno; return -errno;
up_irq_restore(flags);
return ret;
} }
up_irq_restore(flags); ret = host_uninterruptible(fread, buf, 1, len, fp);
ret = host_uninterruptible_errno(fread, buf, 1, len, fp);
host_uninterruptible(pclose, fp); host_uninterruptible(pclose, fp);
} }
return ret; return ret < 0 ? -errno : ret;
} }
/**************************************************************************** /****************************************************************************
@@ -229,6 +223,6 @@ int host_waitpid(pid_t pid)
{ {
int status; int status;
pid = host_uninterruptible_errno(waitpid, pid, &status, 0); pid = host_uninterruptible(waitpid, pid, &status, 0);
return pid < 0 ? pid : status; return pid < 0 ? -errno : status;
} }
+31 -18
View File
@@ -96,15 +96,15 @@ void host_uart_start(void)
{ {
/* Get the current stdin terminal mode */ /* Get the current stdin terminal mode */
host_uninterruptible_no_return(tcgetattr, 0, &g_cooked); tcgetattr(0, &g_cooked);
/* Put stdin into raw mode */ /* Put stdin into raw mode */
host_uninterruptible_no_return(setrawmode, 0); setrawmode(0);
/* Restore the original terminal mode before exit */ /* Restore the original terminal mode before exit */
host_uninterruptible_no_return(atexit, restoremode); atexit(restoremode);
} }
/**************************************************************************** /****************************************************************************
@@ -115,12 +115,16 @@ int host_uart_open(const char *pathname)
{ {
int fd; int fd;
fd = host_uninterruptible_errno(open, pathname, O_RDWR | O_NONBLOCK); fd = open(pathname, O_RDWR | O_NONBLOCK);
if (fd >= 0) if (fd >= 0)
{ {
/* keep raw mode */ /* keep raw mode */
host_uninterruptible_no_return(setrawmode, fd); setrawmode(fd);
}
else
{
fd = -errno;
} }
return fd; return fd;
@@ -132,7 +136,7 @@ int host_uart_open(const char *pathname)
void host_uart_close(int fd) void host_uart_close(int fd)
{ {
host_uninterruptible(close, fd); close(fd);
} }
/**************************************************************************** /****************************************************************************
@@ -145,11 +149,11 @@ int host_uart_puts(int fd, const char *buf, size_t size)
do do
{ {
ret = host_uninterruptible_errno(write, fd, buf, size); ret = write(fd, buf, size);
} }
while (ret < 0 && ret == -EINTR); while (ret < 0 && errno == EINTR);
return ret; return ret < 0 ? -errno : ret;
} }
/**************************************************************************** /****************************************************************************
@@ -162,11 +166,11 @@ int host_uart_gets(int fd, char *buf, size_t size)
do do
{ {
ret = host_uninterruptible_errno(read, fd, buf, size); ret = read(fd, buf, size);
} }
while (ret < 0 && ret == -EINTR); while (ret < 0 && errno == EINTR);
return ret; return ret < 0 ? -errno : ret;
} }
/**************************************************************************** /****************************************************************************
@@ -178,8 +182,12 @@ int host_uart_getcflag(int fd, unsigned int *cflag)
struct termios t; struct termios t;
int ret; int ret;
ret = host_uninterruptible_errno(tcgetattr, fd, &t); ret = tcgetattr(fd, &t);
if (ret >= 0) if (ret < 0)
{
ret = -errno;
}
else
{ {
*cflag = t.c_cflag; *cflag = t.c_cflag;
} }
@@ -196,11 +204,16 @@ int host_uart_setcflag(int fd, unsigned int cflag)
struct termios t; struct termios t;
int ret; int ret;
ret = host_uninterruptible_errno(tcgetattr, fd, &t); ret = tcgetattr(fd, &t);
if (!ret) if (!ret)
{ {
t.c_cflag = cflag; t.c_cflag = cflag;
ret = host_uninterruptible_errno(tcsetattr, fd, TCSANOW, &t); ret = tcsetattr(fd, TCSANOW, &t);
}
if (ret < 0)
{
ret = -errno;
} }
return ret; return ret;
@@ -216,7 +229,7 @@ bool host_uart_checkin(int fd)
pfd.fd = fd; pfd.fd = fd;
pfd.events = POLLIN; pfd.events = POLLIN;
return host_uninterruptible(poll, &pfd, 1, 0) == 1; return poll(&pfd, 1, 0) == 1;
} }
/**************************************************************************** /****************************************************************************
@@ -229,7 +242,7 @@ bool host_uart_checkout(int fd)
pfd.fd = fd; pfd.fd = fd;
pfd.events = POLLOUT; pfd.events = POLLOUT;
return host_uninterruptible(poll, &pfd, 1, 0) == 1; return poll(&pfd, 1, 0) == 1;
} }
/**************************************************************************** /****************************************************************************
-12
View File
@@ -155,18 +155,6 @@
} \ } \
while (0) while (0)
#define host_uninterruptible_errno(func, ...) \
({ \
uint64_t flags_ = up_irq_save(); \
typeof(func(__VA_ARGS__)) ret_ = func(__VA_ARGS__); \
if (ret_ < 0) \
{ \
ret_ = host_errno_convert(-errno); \
} \
up_irq_restore(flags_); \
ret_; \
})
/* File System Definitions **************************************************/ /* File System Definitions **************************************************/
/* These definitions characterize the compressed filesystem image */ /* These definitions characterize the compressed filesystem image */