All of lore.kernel.org
 help / color / mirror / Atom feed
From: Laszlo Ersek <lersek@redhat.com>
To: "Gabriel L. Somlo" <gsomlo@gmail.com>
Cc: jordan.l.justen@intel.com, qemu-devel@nongnu.org,
	gleb@cloudius-systems.com
Subject: Re: [Qemu-devel] fw_cfg specification ?
Date: Wed, 11 Mar 2015 22:30:09 +0100	[thread overview]
Message-ID: <5500B3E1.8020402@redhat.com> (raw)
In-Reply-To: <20150311204537.GW1832@HEDWIG.INI.CMU.EDU>

On 03/11/15 21:45, Gabriel L. Somlo wrote:
> On Wed, Mar 11, 2015 at 07:50:40PM +0100, Laszlo Ersek wrote:
>> On 03/11/15 16:27, Gabriel L. Somlo wrote:
>>> Hi,
>>>
>>> I'm looking for the closest thing to an official spec for qemu's
>>> fw_cfg device, and so far I have found this:
>>>
>>> http://lists.gnu.org/archive/html/qemu-devel/2011-04/msg00238.html
>>>
>>> but it apparently never got committed to qemu (any idea why not?).
>>
>> Must have fallen through the cracks. (Just speculating; in April 2011 I
>> had been with RH for less than half a year, and learning Xen. :))
>>
>>> Googling around didn't get me much further than that.
>>>
>>> Is there anything better or more up to date floating around out
>>> there somewhere ?
>>
>> I won't say "better", but it is "committed": check
>> "Documentation/devicetree/bindings/arm/fw-cfg.txt" in the kernel tree.
>> It is intentionally vague on the set of keys and fw_cfg files that qemu
>> provides, because that's a moving target. You can only rely on the qemu
>> source for those.
> 
> The fw_cfg interface from the guest's perspective is pretty
> straightforward: select something on the control port, read a blob
> from the data port.
> 
> I was more interested in the "hardware" implementation details,
> and the reasoning behind them.
> 
> For instance, I get there's a total of 0x30 entries
> (FWCfgEntry entries), the last 0x10 of which have 
> "file names" and are referenced from the "FWCfgFiles *files"
> structure.
> 
> I don't quite get the part where
> 
>   struct FWCfgState {
>     ...
>     FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
>     ...
>   }
> 
> is accessed via 
> 
>   int arch = !!(key & FW_CFG_ARCH_LOCAL);
>   ...
>   s->entries[arch][key].foo = ...
> 
> I.e., what's the significance of arch==0 vs. arch==1 ?

Well, from that aborted :) text file of mine:

> The client writes a 16-bit wide selector to the control port. The most
> significant bit (FW_CFG_ARCH_LOCAL) selects the namespace: 0
> corresponds to "generic" namespace, while 1 corresponds to
> "architecture specific" namespace.

Jordan had also dedicated a paragraph to Bit15 in his patch that you
linked above.

> Also, what's the significance of handling guest-initiated writes to
> the data port ? Could the guest write larger chunks of data than the
> current size of the selected entry ?

No.

Again, from that aborted text file:

> The client code writes a selector (a key) to the control port, and
> then can read the corresponding data (produced by qemu-kvm) via the
> data port, or, if the selected entry is writable, rewrite it. If
> qemu-kvm has associated a callback with the entry: when the entry is
> completely rewritten, qemu-kvm runs the callback. This way the client
> code can exchange data with and even invoke functions in qemu-kvm.

> Would the selected entry's size
> grow accordingly ? Would it shrink if the guest (over)wrote less than
> the current size ?

No. Please see the fw_cfg_write() function. Only the same size can be
written, and the write callback runs when that's completed.

In any case, I'm unaware of *any* instance when an fw_cfg blob is
rewritten by the guest.

In fact, such *write* callbacks are registered in qemu with
fw_cfg_add_callback(), and I can't see any calls to that function. It's
dead code, apparently.

(fw_cfg_add_file_callback() is different; it is much more recent, and it
is for read callbacks.)

> I know it's all going to start making sense eventually, if I stare at
> the source long enough :) I'm just trying to speed up that process as
> much as possible.
> 
> Right now, at a quick glance, qemu writes a bunch of stuff into the
> fw_cfg data structure before starting the guest, and the guest (well,
> mostly the guest's BIOS) does a bunch of things based on the
> "breadcrumbs" it reads from the fw_cfg device.
> 
> But that doesn't seem to be the whole story...

It is pretty close; it's almost the whole story, as far as I'm aware.
What more should an interface called "firmware config" do than, well,
configure the firmware? :)

(Even -kernel / -initrd / -append abuse the original intent of fw_cfg,
because those blobs are huge, and not configuration-like at all. But
they got popular and now we're stuck with them; higher level tools
depend on them heavily.)

There is though a bit more to the story: the (recent) read callbacks.
Qemu sometimes decides to re-generate a "bunch of stuff" in the fw_cfg
blobs. See the commit message here:

https://github.com/tianocore/edk2/commit/66b280df

(The edk2 patch visible in that commit has been completely reimplemented
since, but the qemu description in the commit *message* remains valid.)

>> If you have a ton of time, you could try documenting fw_cfg yourself,
>> but as I said, it's a moving target, so the description would either
>> become stale quickly, or require people to keep it in sync with the
>> source all the time. Updating documentation sucks *hard*.
> 
> Once I start getting what's going on (e.g., w.r.t. the questions above)
> I wouldn't mind just adding *comments* to the source, for the next guy
> who, like me, is trying to get the lay of the land, but I'm not there
> yet...

I believe that would be useful, yes.

Thanks
Laszlo

  reply	other threads:[~2015-03-11 21:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-11 15:27 [Qemu-devel] fw_cfg specification ? Gabriel L. Somlo
2015-03-11 18:50 ` Laszlo Ersek
2015-03-11 20:45   ` Gabriel L. Somlo
2015-03-11 21:30     ` Laszlo Ersek [this message]
2015-03-12  7:47       ` Gerd Hoffmann
2015-03-12 14:17       ` Paolo Bonzini
2015-03-12 15:42         ` Laszlo Ersek
2015-03-12 16:16           ` Gabriel L. Somlo
2015-03-12 16:35             ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5500B3E1.8020402@redhat.com \
    --to=lersek@redhat.com \
    --cc=gleb@cloudius-systems.com \
    --cc=gsomlo@gmail.com \
    --cc=jordan.l.justen@intel.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.