From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.gentoo.org (woodpecker.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) by sourceware.org (Postfix) with ESMTP id E4FAD385783A; Sun, 25 Apr 2021 23:34:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E4FAD385783A Received: from vapier (localhost [127.0.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id CF90534171F; Sun, 25 Apr 2021 23:34:49 +0000 (UTC) Date: Sun, 25 Apr 2021 19:34:49 -0400 From: Mike Frysinger To: Michael Eager Cc: Joel Sherrill , gdb@sourceware.org, Newlib Subject: Re: Microblaze libgloss and gdb simulator Message-ID: Mail-Followup-To: Michael Eager , Joel Sherrill , gdb@sourceware.org, Newlib References: <30fc7511-5f60-e611-d732-57cdaad80107@eagercon.com> <6d72dbe2-efe3-f8ba-3c2b-0c248883649b@eagercon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <6d72dbe2-efe3-f8ba-3c2b-0c248883649b@eagercon.com> X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 25 Apr 2021 23:34:55 -0000 On 25 Apr 2021 15:29, Michael Eager wrote: > On 4/25/21 1:27 PM, Mike Frysinger wrote: > > On 25 Apr 2021 11:37, Michael Eager wrote: > >> On 4/25/21 10:21 AM, Mike Frysinger wrote: > >>> On 24 Apr 2021 01:02, Mike Frysinger via Gdb wrote: > >>>> On 22 Apr 2021 21:19, Mike Frysinger via Gdb wrote: > >>>>> On 22 Apr 2021 19:55, Joel Sherrill wrote: > >>>>>> On Thu, Apr 22, 2021, 7:27 PM Mike Frysinger wrote: > >>>>>>> ignoring that, the microblaze sim doesn't have syscall support hooked up. > >>>>>>> so it's only a CPU simulator atm. > >>>>>> > >>>>>> So it has no output whatsoever? Does it get used for anything? > >>>>> > >>>>> afaict, correct. the most basic sims just do CPU level stuff and then have > >>>>> their state inspected, or communicate pass/fail via exit status or abort. > >>>>> this behavior actually isn't that rare ... it's where most sims start. > >>>>> > >>>>>> We are resurrecting some old work that I did for a Microblaze port. I did > >>>>>> write an inbyte() and outbyte() which would normally come from the xil > >>>>>> library. But I don't have any idea how I figured out there was a uart at a > >>>>>> particular address. I swear I had it working to print then but now it > >>>>>> faults after the first instruction. > >>>>>> > >>>>>> Is there any known good executable for it? Even just seeing it operate with > >>>>>> a linked executable that had a crt0 and did something would be helpful at > >>>>>> this point. > >>>>> > >>>>> ftr, i've never worked on microblaze. i'm just reading the code and poking > >>>>> the toolchain :). > >>>>> > >>>>> getting i/o (or maybe just o) support into the sim shouldn't be terribly hard. > >>>>> we could even do the normal libgloss syscalls. the important things we need to > >>>>> know are: > >>>>> * how does outbyte work ? is it writing to MMIO UARTs, or something else ? > >>>>> * is there an interrupt or exception or specific insn that microblaze uses to > >>>>> trigger the hypervisor/monitor/whatever ? if so, should be possible to wire > >>>>> that up in the microblaze port. my reading of libgloss/microblaze/ isn't > >>>>> picking out anything interesting, but i'm by no means an expert here. > >>>>> > >>>>> if you can figure out those bits, happy to help on the sim side. > >>>> > >>>> here's wiring up the syscall path, but it's unclear whether we should do this > >>>> without changes landing in libgloss first. > >>> > >>> Michael: what do you think of wiring up the libgloss syscalls for microblaze > >>> via the brki insn ? is there any prior art in this space for microblaze ? > >>> i wouldn't want to try an allocate syscall space that's already used. > >> > >> I never did anything with the SIM code for Microblaze. I always used a > >> proprietary instruction set simulator provided by Xilinx. > > > > the git history says it came from you :) > > https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=bd30e45a3430ce30c63146aa8cad0796754581b6 > > Yeah. Other than submitting it upsteam, I don't think I > did anything with the simulator. > > >> I would not use a BRKI instruction, since the ABI has documented > >> its behavior. Someone might write code which depends on this. > >> > >> Microblaze doesn't have a system call instruction. I would pick an > >> undefined op code and treat this as a SYS instruction, interpreting > >> it as call to libC or other system functionality. > >> > >> This may (or may not) be helpful: I implemented something similar to > >> this in QEMU for Qualcomm/Ubicom, where a SYSCALL (or some such) > >> instruction was translated into read, write, and other system calls. It > >> was modeled on similar semihosting support in ARM, if I recall > >> correctly. IIRC, functions in libgloss were thin wrappers around > >> the SYSCALL instruction. > > > > the manual says for BRKI: > > Application (user-mode) programs transfer control to system-service routines > > (privileged mode programs) using the BRALID or BRKI instruction, jumping to > > physical address 0x8. Executing this instruction causes a system-call exception > > to occur. The exception handler determines which system-service routine to call > > and whether the calling application has permission to call that service. > > If permission is granted, the exception handler performs the actual procedure > > call to the systemservice routine on behalf of the application program. > > ... > > When MicroBlaze is configured to use an MMU (C_USE_MMU >= 1) this instruction > > is privileged, except as a special case when "brki rD, 0x8" or "brki rD, 0x18" > > is used to perform a Software Break. This means that, apart from the special > > case, if the instruction is attempted in User Mode (MSR[UM] = 1) a Privileged > > Instruction exception occurs. > > > > isn't this a syscall handler ? this is how Linux implements things too. > > https://man7.org/linux/man-pages/man2/syscall.2.html#NOTES > > microblaze brki r14,8 r12 r3 - - > > > > libgloss has: > > # define SYSCALL_BODY(name) \ > > addik r12, r0, SYS_ ## name; \ > > brki r14, 8; \ > > rtsd r15, 8; \ > > nop; > > It depends on what code you are simulating. > > If you are simulating a program which uses libgloss and you expect the > behavior of brki to be a syscall, then this is a very reasonable way > to implement system calls. Instead of taking the branch to the handler, > the simulator does whatever host system call it is supposed to do, then > returns, just as if there was an OS handling the system call. (This is > what QEMU calls "semihosting".) yes, this is exactly what we were thinking. i wasn't aware of the QEMU terminology, thanks. in the GNU sim, we call them "environments". the default is the virtual environment which behaves this way: there is only one privilege level, and any exception processing is handled entirely in the sim itself. > I was thinking about the more general case, where you might be > simulating an image which includes an exception handler. In this > case, the behavior you want is to take the exception and jump to the > excption handler address, instead of the simlator interpreting the > brki as a syscall. absolutely this makes sense. this would be the "operating" environment where the sim wouldn't hijack anything, and it would be responsible for emulating the various privilege levels such as setting mode bits and transferring control to any registered exception handlers. this would be useful for e.g. fully simulating the Linux kernel & userland. it sounds like we're in agreement. if there's anyone you think we could or should consult before moving forward, please loop them in. -mike