mfld-fr / emu86

Intel IA16 emulator for embedded development
37 stars 6 forks source link

Console mode does not work on Linux #12

Closed mfld-fr closed 4 years ago

mfld-fr commented 4 years ago

I tried for the first time the 'console' mode on commit 1e25716, and I got the following error on Gentoo Linux:

mfld@home /mnt/data/home/mfld/advantech/mon86/target $ ../../emu86/emu86 -w 0x80000 -f ../../img/ADVTECH1.BIN -w 0xf8100 -f mon86.bin -c 0xf8100
info: load address 80000h
info: load file ../../img/ADVTECH1.BIN
info: file size=80000h
success: file loaded
info: load address F8100h
info: load file mon86.bin
info: file size=B2Ch
success: file loaded
info: code breakpoint address F8100h
warning: writing byte into ROM @ f0692h
warning: cannot read from console:: Resource temporarily unavailable
error: execute operation
0060:472A  CD 16              int     $0x16
>q

The same run works fine in 'pty' mode.

ghaerr commented 4 years ago

warning: cannot read from console:: Resource temporarily unavailable

I'll fix this, but don't have access to my debian box until Tuesday.

It appears this issue is related to user limits, although I'm unable to guess what it might be... Can you run "ulimit -aH" or "prlimit" to see what resource is being restricted?

mfld-fr commented 4 years ago

ulimit:

mfld@home /mnt/data/home/mfld/advantech/emu86 $ ulimit -aH
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 63852
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 4096
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) unlimited
cpu time               (seconds, -t) unlimited
max user processes              (-u) 63852
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

prlimit:

mfld@home /mnt/data/home/mfld/advantech/emu86 $ prlimit
RESOURCE   DESCRIPTION                               SOFT        HARD UNITS
AS         address space limit                sans limite sans limite octets
CORE       max core file size                           0 sans limite octets
CPU        CPU time                           sans limite sans limite secondes
DATA       max data size                      sans limite sans limite octets
FSIZE      max file size                      sans limite sans limite octets
LOCKS      max number of file locks held      sans limite sans limite verrous
MEMLOCK    max locked-in-memory address space       65536       65536 octets
MSGQUEUE   max bytes in POSIX mqueues              819200      819200 octets
NICE       max nice prio allowed to raise               0           0 
NOFILE     max number of open files                  1024        4096 fichiers
NPROC      max number of processes                  63852       63852 processus
RSS        max resident set size              sans limite sans limite octets
RTPRIO     max real-time priority                       0           0 
RTTIME     timeout for real-time tasks        sans limite sans limite microsecondes
SIGPENDING max number of pending signals            63852       63852 signaux
STACK      max stack size                         8388608 sans limite octets
ghaerr commented 4 years ago

ulimit / prlimit

Thanks @mfld-fr. I've tracked this down to something to do with Linux's special handling of stdin when set to non-blocking mode, which is different between the console and pty drivers. More coming when I can fully reproduce on Linux vs macOS.

mfld-fr commented 4 years ago

@ghaerr : OK, thanks, let us wait for your test on your Linux box...

ghaerr commented 4 years ago

@mfld-fr :

I just tested on two emulated Linux VMs, Ubuntu 18.10 running kernel 4.18.0-16 and Debian 9.9. Both versions did not reproduce this error. I presume this error is occurring immediately after loading with the arguments you've specified, NOT after a ^C, correct?

Can you give me the version of Gentoo you are running on, I may have to download that specific version.

A discussion on the possible reasons for this error are here https://github.com/fish-shell/fish-shell/issues/176#issuecomment-15807928, although the issue could be to do with the shell, as discussed in detail here https://www.cons.org/cracauer/sigint.html.

In the meantime, I've discovered two more problems associated with the console driver:

I will submit a PR for these two problems.

ghaerr commented 4 years ago

@mfld-fr : I downloaded the base Gentoo from their main site and setup a VM, to try to reproduce this ug. The system doesn't come with a compiler. Can you give me the kernel version of your Gentoo system? Did you download a full system from somewhere, or have you built up your Gentoo from scratch?

mfld-fr commented 4 years ago

I build my Gentoo system following the instructions of the manual : https://wiki.gentoo.org/wiki/Handbook:Main_Page

Here is the detailled configuration:

mfld@home ~ $ emerge --info
Portage 2.3.103 (python 3.7.8-final-0, default/linux/amd64/17.1/desktop, gcc-9.3.0, glibc-2.30-r8, 5.4.38-gentoo-x86_64 x86_64)
=================================================================
System uname: Linux-5.4.38-gentoo-x86_64-x86_64-Intel-R-_Core-TM-_i3-9100F_CPU_@_3.60GHz-with-gentoo-2.6
KiB Mem:    16349368 total,   9610784 free
KiB Swap:    4194300 total,   4194300 free
Timestamp of repository gentoo: Fri, 31 Jul 2020 18:45:01 +0000
Head commit of repository gentoo: 3727fcce65581b0fb086ad4cdd5a4d711d869fbc
sh bash 5.0_p17
ld GNU ld (Gentoo 2.33.1 p2) 2.33.1
app-shells/bash:          5.0_p17::gentoo
dev-java/java-config:     2.3.1::gentoo
dev-lang/perl:            5.30.3::gentoo
dev-lang/python:          2.7.18-r1::gentoo, 3.7.8-r2::gentoo, 3.8.4-r1::gentoo
dev-util/cmake:           3.16.5::gentoo
dev-util/pkgconfig:       0.29.2::gentoo
sys-apps/baselayout:      2.6-r1::gentoo
sys-apps/openrc:          0.42.1::gentoo
sys-apps/sandbox:         2.18::gentoo
sys-devel/autoconf:       2.13-r1::gentoo, 2.69-r4::gentoo
sys-devel/automake:       1.16.1-r1::gentoo
sys-devel/binutils:       2.33.1-r1::gentoo
sys-devel/gcc:            9.3.0-r1::gentoo
sys-devel/gcc-config:     2.3.1::gentoo
sys-devel/libtool:        2.4.6-r6::gentoo
sys-devel/make:           4.2.1-r4::gentoo
sys-kernel/linux-headers: 5.4-r1::gentoo (virtual/os-headers)
sys-libs/glibc:           2.30-r8::gentoo
Repositories:

gentoo
    location: /mnt/data/usr/portage
    sync-type: rsync
    sync-uri: rsync://host.mfld.fr/gentoo-portage
    priority: -1000
    sync-rsync-verify-max-age: 24
    sync-rsync-verify-jobs: 1
    sync-rsync-extra-opts: 
    sync-rsync-verify-metamanifest: no

local
    location: /usr/local/portage
    masters: gentoo

ACCEPT_KEYWORDS="amd64"
ACCEPT_LICENSE="* -@EULA"
CBUILD="x86_64-pc-linux-gnu"
CFLAGS="-march=core2 -O2 -pipe"
CHOST="x86_64-pc-linux-gnu"
CONFIG_PROTECT="/etc /usr/lib64/libreoffice/program/sofficerc /usr/share/gnupg/qualified.txt"
CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/dconf /etc/env.d /etc/fonts/fonts.conf /etc/gconf /etc/gentoo-release /etc/revdep-rebuild /etc/sandbox.d /etc/terminfo"
CXXFLAGS="-march=core2 -O2 -pipe"
DISTDIR="/usr/portage/distfiles"
ENV_UNSET="DBUS_SESSION_BUS_ADDRESS DISPLAY GOBIN GOPATH PERL5LIB PERL5OPT PERLPREFIX PERL_CORE PERL_MB_OPT PERL_MM_OPT XAUTHORITY XDG_CACHE_HOME XDG_CONFIG_HOME XDG_DATA_HOME XDG_RUNTIME_DIR"
FCFLAGS="-O2 -pipe"
FEATURES="assume-digests binpkg-docompress binpkg-dostrip binpkg-logs config-protect-if-modified distlocks ebuild-locks fixlafiles getbinpkg ipc-sandbox merge-sync multilib-strict network-sandbox news parallel-fetch pid-sandbox preserve-libs protect-owned qa-unresolved-soname-deps sandbox sfperms strict unknown-features-warn unmerge-logs unmerge-orphans userfetch userpriv usersandbox usersync xattr"
FFLAGS="-O2 -pipe"
GENTOO_MIRRORS="ftp://ftp.free.fr/mirrors/ftp.gentoo.org/"
LANG="fr_FR.utf8"
LDFLAGS="-Wl,-O1 -Wl,--as-needed"
LINGUAS="en fr"
MAKEOPTS="-j2"
PKGDIR="/usr/portage/packages"
PORTAGE_BINHOST="ftp://host.mfld.fr/"
PORTAGE_CONFIGROOT="/"
PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --omit-dir-times --compress --force --whole-file --delete --stats --human-readable --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages --exclude=/.git"
PORTAGE_TMPDIR="/var/tmp"
USE="X a52 aac acl acpi alsa amd64 berkdb bindist branding bzip2 cairo cdda cdr cli crypt cups dbus dri dts dvd dvdr elogind encode exif flac fortran gdbm gif gimp gpm gtk iconv icu jpeg lcms libnotify libtirpc lm-sensors mad mng mp3 mp4 mpeg mtp multilib ncurses nls nptl ogg opengl openmp pam pango pcre pdf png policykit ppds pulseaudio qt5 readline scanner sdl seccomp spell split-usr ssl startup-notification svg tcpd theora tiff truetype udev udisks unicode upower usb v4l vorbis wxwidgets x264 xattr xcb xml xv xvid zlib" ABI_X86="64" ADA_TARGET="gnat_2018" ALSA_CARDS="hda-intel" APACHE2_MODULES="authn_core authz_core socache_shmcb unixd actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cgi cgid dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" CALLIGRA_FEATURES="karbon sheets words" COLLECTD_PLUGINS="df interface irq load memory rrdtool swap syslog" CPU_FLAGS_X86="mmx mmxext sse sse2 sse3 sse4_1 ssse3" ELIBC="glibc" GPSD_PROTOCOLS="ashtech aivdm earthmate evermore fv18 garmin garmintxt gpsclock greis isync itrax mtk3301 nmea ntrip navcom oceanserver oldstyle oncore rtcm104v2 rtcm104v3 sirf skytraq superstar2 timing tsip tripmate tnt ublox ubx" INPUT_DEVICES="evdev" KERNEL="linux" L10N="en fr" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" LIBREOFFICE_EXTENSIONS="presenter-console presenter-minimizer" OFFICE_IMPLEMENTATION="libreoffice" PHP_TARGETS="php7-2 php7-3" POSTGRES_TARGETS="postgres10 postgres11" PYTHON_SINGLE_TARGET="python3_7" PYTHON_TARGETS="python2_7 python3_7" RUBY_TARGETS="ruby25" SANE_BACKENDS="plustek" USERLAND="GNU" VIDEO_CARDS="amdgpu radeonsi" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipset ipp2p iface geoip fuzzy condition tee tarpit sysrq steal rawnat logmark ipmark dhcpmac delude chaos account"
Unset:  CC, CPPFLAGS, CTARGET, CXX, EMERGE_DEFAULT_OPTS, INSTALL_MASK, LC_ALL, PORTAGE_BUNZIP2_COMMAND, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, PORTAGE_RSYNC_EXTRA_OPTS
ghaerr commented 4 years ago

@mfld-fr :

mfld@home /mnt/data/home/mfld/advantech/mon86/target $ ../../emu86/emu86 -w 0x80000 -f ../../img/ADVTECH1.BIN -w 0xf8100 -f mon86.bin -c 0xf8100
info: load address 80000h
info: load file ../../img/ADVTECH1.BIN
info: file size=80000h
success: file loaded
info: load address F8100h
info: load file mon86.bin
info: file size=B2Ch
success: file loaded
info: code breakpoint address F8100h
warning: writing byte into ROM @ f0692h
warning: cannot read from console:: Resource temporarily unavailable
error: execute operation
0060:472A  CD 16              int     $0x16
>q

I am pretty sure this error has nothing to do with Gentoo specifically, as I can't get it to reproduce on any system. This is because I don't (yet) have ADVTECH1.BIN nor mon86.bin to load. Instead, I think the problem has to do with the emulated MON86 code or the EMU86 BIOS emulation code calling a BIOS function that calls serial_recv, expecting the system to "wait" on a character. This will need some thinking and a slight redesign in order to implement on my console code.

Notice that you have a ">" prompt above, and 'q' was used to exit. This means the emu-console code is running properly. The error "warning: cannot read from console:: Resource temporarily unavailable" is very likely because, for the console driver (and unlike the PTY driver), a serial-poll() call must be used before calling serial_read(). This is because, for the new console driver, the console is in non-blocking mode, whereas in PTY, there is no need for such.

Thus, the "Resource temporarily unavailable" is actually essentially a -EWOULDBLOCK return. In this case, the fix entails testing for the specific errno return value occurring in your case, and returning -1 (or 0xFF, depending on your serial_recv() version), and continuing.

I am still studying MON86 but am sure it calls a function to wait on a character, which ends up calling serial_recv().

We need to (re)define the semantics of serial_poll and serial_recv, given this information. If we can determine the BIOS call that mon86 is making to the Advantech BIOS, I can duplicate this on my system, and then proceed to provide a fix where the system waits for a read, hopefully not by busy looping, to implement this functionality.

Apart from this, you should be able to test EMU86 with my console driver by running the emulator as above with -i, and start immediately single stepping, with the console operational. The error will occur when the system later calls serial_recv() without a character ready, as described.

Please advise, thanks!

mfld-fr commented 4 years ago

Fixed by #17